home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Games / JST / sources / OSEmu / osemu.asm < prev    next >
Encoding:
Assembly Source File  |  2001-03-19  |  109.9 KB  |  4,893 lines

  1. ;*---------------------------------------------------------------------------
  2. ;  :Program.    osemu.asm
  3. ;  :Author.    Harry, Wepl
  4. ;  :RCS.    $Id: osemu.asm 1.1 1999/02/03 04:10:48 jotd Exp jotd $
  5. ;  :History.    all work before history starts done by Harry
  6. ;    30.06.98   Wepl     rework started
  7. ;    06.07.98   Wepl     rework finished
  8. ;    07.07.98   Wepl     exec.Supervisor,exec.SuperState,exec.UserState
  9. ;                    exec.FindTask,exec.FindPort,exec.OpenDevice
  10. ;                    graphics.alot,exec.DoIO
  11. ;    15.07.98   Wepl     work for Deuteros finished
  12. ;    31.07.1998 Harry    SSP set on memtop again as it is in kick1.3
  13. ;                possibility of trainer routine added
  14. ;    10.08.98   Wepl     osemu header struct changed (SLAVETRAINER now
  15. ;                in osemu-struct instead overgiven via reg)
  16. ;    10.08.98   Wepl     gfx.BltBitMap fixed
  17. ;    ??.08.98   MrLarmer some additions
  18. ;    05.09.98   Wepl     AddIntServer/RemIntServer finally fixed
  19. ;                ReadPixel,WritePixel added
  20. ;    06.09.98   Wepl     return values after init changed (bootblock)
  21. ;                BltTemplate started (but unfinished)
  22. ;    22.09.98   Wepl     _SendIO is now the same as _DoIO (fix for Deuteros)
  23. ;    27.09.98   Harry    dos.lock (untested), device-names even-
  24. ;                aligned (68000-prob), (usp) initialized 
  25. ;                with stacksize (CC3 expects it in 4(usp) at
  26. ;                start), dummy: dos.input+dos.output+
  27. ;                exec.waitport
  28. ;    29.09.1998 Harry    list management functions implemented: 
  29. ;                exec.insert, exec.(add|rem)(head|tail), exec.remove
  30. ;    07.10.1998 Harry    list handling debugged, libs as list, one process
  31. ;                structure, exec.findtask changed, _initlibary
  32. ;                splittet, exec.getmsg, dos.deviceproc, intuition.
  33. ;                openwindow, intuition.closewindow, icon.library,
  34. ;                intuition.cleardmrequest, dos.unloadseg, 
  35. ;                minor fixes
  36. ;    11.10.1998 Harry    devices as list, dos.loadseg supports hunk_symbol,
  37. ;                audio.device-dummy, gfx.allocraster, gfx.freeraster,
  38. ;                gfx.bltclear, gfx.initbitmap (and #fe), intuition.
  39. ;                lockibase dummy, layers.library, mathffp.library,
  40. ;                trackdisk.device-msgport, execlib-table implemented
  41. ;                directly, exec.copymem, exec.findname, minor fixes
  42. ;    19.10.1998 Harry    dos.examine, dos.unlock, exec.setintvector,
  43. ;                audio-interrupts, gfx.getcolormap
  44. ;    20.10.1998 Harry    gfx.set(a|b)pen, gfx.setdrmd, gfx.initrastport,
  45. ;                topaz.font-replacement, gfx.openfont, gfx.setfont,
  46. ;                some gfx-structures
  47. ;    24.10.1998 Harry    gfx.text, some debugging, exec.allocsignal, 
  48. ;                gfx.move, problem with input.addhandler, 
  49. ;                exec.availmem keeps now a reserve of $1000, 
  50. ;                gfx.freecolormap
  51. ;    31.10.98   Wepl     input.device fixed, color handling changed 
  52. ;                (getcolormap,freecolormap,loadrgb4,initview,
  53. ;                initvport,makevport,loadview,setrgb4,getsprite,
  54. ;                freesprite,movesprite,changesprite,vbi
  55. ;    03.11.1998 Wepl     bug in AllocMem fixed (size rounded up now)
  56. ;    25.11.1998 Harry    switched back to old gfx-viewport-stuff,
  57. ;                gfx.loadview(0)
  58. ;       30.11.1998 MrLarmer exec.(create|delete)(iorequest|msgport),
  59. ;                exec.freesignal, exec.allocsignal changed
  60. ;    01.12.1998 Harry    libs in osemu extended to V40 (as i dont have 
  61. ;                3.1-includes working with asm-one do not have 
  62. ;                the idea to replace the values by labels,
  63. ;                ill change them back!), exec.exitintr
  64. ;       05.12.1998 Harry    some debugging, list handling debugged again,
  65. ;                           exec.allocmem:MEMF_LARGEST dummy, MEMF_REVERSE
  66. ;                dummy, exec.setsignal, dos.currentdir, 
  67. ;                dos.waitforchar, execbase.vblankfrequency,
  68. ;                dos.duplock
  69. ;    09.01.1999 JOTD     bugfix in DoIO() return code (D0). "implemented"
  70. ;                           CMD_UPDATE (does nothing) and CMD_CLEAR
  71. ;    09.01.1999 JOTD     improved CloseWorkbench() intuition call
  72. ;                           Now returns 0 only once, and then 1
  73. ;                           Led Storm does not lock anymore now
  74. ;       10.01.1999 JOTD     added AlohaWorkbench() intuition call (dummy)
  75. ;                           added WaitBOFVP() (not accurate)
  76. ;                           added CloseScreen() (dummy)
  77. ;       14.01.1999 JOTD     added dos.library Info() function
  78. ;                           (for Flashback)
  79. ;       15.01.1999 JOTD     added D0=0 return codes in input.device
  80. ;       15.01.1999 JOTD     added lowlevel.library, nonvolatile.library
  81. ;                           added cd.device and freeanim.library (dummies)
  82. ;                           added exec CheckIO
  83. ;                           improved DoIO (dummy support of cd.device,cdtv.device)
  84. ;                           added WaitIO (dummy support of cd.device,cdtv.device)
  85. ;                           added SetFunction (for Banshee, now intro OK)
  86. ;       30.01.1999 JOTD     added keyboard.device (not complete)
  87. ;                           installed a default empty copperlist to avoid problems
  88. ;                           with JST
  89. ;       02.02.1999 JOTD     added complete lowlevel.library ReadJoyPort() function
  90. ;                           using function keys (F1-F5) for joypad extra buttons
  91. ;                           DoIO/SendIO: if device is not found, then return with
  92. ;                           no error (dummy for cd.device)
  93. ;                           Oscar CD32 now works perfectly!
  94. ;       18.03.1999 MrLarmer keyboard.device improved
  95. ;                       FreeSignal improved
  96. ;
  97. ;       30.03.1999 JOTD     Added graphics.library InitTmpRas, InitArea
  98. ;       01.04.1999 JOTD     Added mathffp.library SPFlt,SPDiv (removed
  99. ;                           from intuition.s)
  100. ;       01.04.1999 JOTD     Added mathtrans.library SPATan (untested)
  101. ;       17.04.1999 JOTD     Dummy console.device added
  102. ;       19.05.1999 JOTD     Added CMD_WRITE to trackdisk DoIO (for ArcadePool)
  103. ;                           Added dummy entry for nonvolatile StoreEnv
  104. ;                           Added items in the OSEmu structure for use with JST
  105. ;                           Added OSEmu ID, version/release and version string
  106. ;                           Added whdmacros.i include for v10 WHDLoad .i files
  107. ;       26.05.1999 JOTD     Added EntryType to FileInfoBlock structure
  108. ;                           (Sensible Soccer CD32 v1.2 needed 2 $FFFF in struct
  109. ;                           to get hold of the filesize, the bastards!)
  110. ;       08.06.1999 JOTD     Added fastmemory support, debug entry (HRTMon)
  111. ;                           Added flush cache after LoadSeg() succeeded (v1.4)
  112. ;       11.06.1999 JOTD     Corrected LoadSeg() to allocate hunks in chip/fast (v1.5)
  113. ;    26.06.1999 JOTD     Added dummy AssignPath()
  114. ;                           Added disk.resource OpenResource() (no functions yet)
  115. ;                           Fixed a bug in Examine if not found (FreeMem problem)
  116. ;                           Added ExamineFH (SlamTilt)
  117. ;                           Added AllocRemember/FreeRemember (SlamTilt)
  118. ;       10.08.1999 JOTD     Added dummy disk.resource GetUnit()
  119. ;                           Added AllocEntry/FreeEntry (on Bored Seal request) (v1.7)
  120. ;       24.09.1999 JOTD     Bugfixed DupLock() (v1.8)
  121. ;    12.02.2000 JOTD        Added dummy RemakeDisplay for Darkmere (v1.9)
  122. ;                           LoadSeg() bugfix: zero-length hunks are allowed
  123. ;       21.02.2000 Harry    Copperinterrupt implemented, Audioint and 
  124. ;                           Add/RemIcrVector fixed, minor changes (v1.10)
  125. ;                        Added HUNK_DEBUG support to LoadSeg()
  126. ;       02.03.2000 JOTD     Added dummy IoErr() (v1.11)
  127. ;       12.04.2000 JOTD     Improved IoErr() (v1.12)
  128. ;                           Added dos.library ExNext() (don't know if it works well)
  129. ;                           Changed dos.library Examine() for Volumes. Maybe I shouldn't have.
  130. ;                           Added exec.library TypeOfMem() for Subwars 2050
  131. ;                           Added dummy keymap.library, diskfont.library, utility.library
  132. ;       14.04.2000 JOTD     Thanks to Harry, fixed gfx library copper stuff: OSEmu is much
  133. ;                           smaller now, and inserted a define to disabled math libs,
  134. ;                           rather unused (v1.13)
  135. ;       18.05.2000 JOTD     Rewritten UCopListInit (untested)
  136. ;                           Added start of Examine/ExNext, a bit buggy, so ExNext disabled
  137. ;                           Added a valid Taskwait in exec structure
  138. ;                           Added intuition's OpenScreen
  139. ;                           Added intuition's MakeScreen
  140. ;                           Added lame dos DateStamp
  141. ;                           Added dos CreateProc
  142. ;                           Added dummy timer.device (in work)
  143. ;                           Added OpenFromLock (easy as Lock & FH struct are the same for OSEmu)
  144. ;                           Added exec's AllocVec, FreeVec
  145. ;                           Changed version strings (v1.14)
  146. ;       05.06.2000 JOTD     Big completions done on Process structure and Dos Library structure
  147. ;                           Now it is possible to simulate autodetachable code (Delphine programs)
  148. ;                           Added dummy GetDiskObject
  149. ;                           Added lowlevel.library AddTimerInt, and cia's AbleICR
  150. ;                           but does not work yet (and was a dirty resource of ROMs :) )  (v1.15)
  151. ;       10.06.2000 JOTD     Added buffered file write (DOSSAVE) (v1.16)
  152. ;                           DosLock & DosFileHandle structure code cleanup for
  153. ;                           future BCPL conversion and real structure offsets
  154. ;       07.09.2000 JOTD     Fixed _strcmp bug which impacted on FindName()
  155. ;                           Changed source to compile with WHDLoad v12 include (patch) (v1.17)
  156. ;       13.09.2000 JOTD     Added StackSwap (direct 3.1 ROM resource) (v1.18)
  157. ;       24.09.2000 JOTD     Better fake dos.Output() implementation
  158. ;                           Set iv_Data #$D to GfxBase in ExecBase
  159. ;                           (games expect this to restore clist)
  160. ;                           Improved SetIntVector (direct (almost!) 3.1 ROM resource)
  161. ;                           Added Blitter interrupt support for AddIntServer/SetIntVector
  162. ;                           Now miracle: Banshee graphics & sound work OK !!!
  163. ;                           Fixed AddIntServer: now activates the required interrupt (v1.19)
  164. ;       29.09.2000 JOTD     Fixed intuition.AllocRemember and FreeRemember
  165. ;                           Now SlamTilt works 100% (v1.20)
  166. ;       30.09.2000 JOTD     CLI.CommandName updated on a dos.LoadSeg
  167. ;                           dos.Open, dos.Close, dos.Lock, etc... use BCPL pointers
  168. ;                           (some games like to read in the filehandler structure...)
  169. ;                           Added utility.library support for 68020+ only (multiply
  170. ;                           and divide functions)
  171. ;                           Added diskfont.library OpenDiskFont() (calls OpenFont()
  172. ;                           with topaz8, what a shame).
  173. ;                           Added graphics.library lame TextLength()
  174. ;                           Improved exec.Enable and exec.Disable (nest count support) (v1.21)
  175. ;       02.10.2000 JOTD     Bugfix in BCPL pointer handling
  176. ;                           ExNext implemented. Now directory scanning works
  177. ;                           Improved intuition.library opening sequence, a default screen
  178. ;                           now opens
  179. ;                           dos.LoadSeg() now also returns seglist in D1
  180. ;                           intuition.OpenScreen() improved
  181. ;                           graphics.GetRGB4() implemented. still in test (v1.22)
  182. ;       15.10.2000 JOTD     intuition.OpenScreenTagList() implemented (Pinball Illusions)
  183. ;                           SetIntVector now accepts copper interrupts (Frontier)
  184. ;                           Fixed CLI.CommandName value (Frontier)
  185. ;                           Added intuition.ViewAddress()
  186. ;                           Added dummy dos.IsInteractive() (returns FALSE)
  187. ;                           Added dummy intuition.SetPointer()
  188. ;                           Removed some copperlist diwstrt, diwstop I introduced
  189. ;                           some time ago for Banshee
  190. ;                           Added dos.DeleteFile() for Frontier
  191. ;                           Totally rewrote volume/directory management in dos.s
  192. ;                           (the cmp #':' test was not enough) (v1.23)
  193. ;       16.10.2000 JOTD     Added CurrentDir support, now Guardian works!
  194. ;                           Added dummy locale.OpenLocale, Fighting Spirit works
  195. ;                           Added exec.CopyMemQuick            
  196. ;                           Fixed a LoadSeg() bug I introduced in v1.23 (v1.24)
  197. ;       20.10.2000 JOTD     Added AllocBitMap (direct OS3.1 resource)
  198. ;                           dos.LoadSeg() now supports overlays!
  199. ;                           more permissive AllocMem (D1 now allows trash, cf Test Drive II)
  200. ;                           Debugged AllocVec/FreeVec (it did not work at all!)
  201. ;                           LoadSeg() now makes WHDLoad/JST fail if problems with structure
  202. ;                           Fixed exec.FreeEntry
  203. ;                           Added intuition.ViewPortAddress() (Lost Vikings)
  204. ;                           Added disk.resource.GiveUnit() (dummy!) (v1.25)
  205. ;       31.10.2000 JOTD     Added some mathffp.library calls
  206. ;                           Added exec.GetCC()
  207. ;                           Improved intuition initialization
  208. ;                           Fixed OpenWindow. Killerball now works (v1.26)
  209. ;       02.11.2000 JOTD     Protected CurrentDir against file locks
  210. ;                           Fixed DupLock/OpenFromLock for root lock
  211. ;                           Bugfixed OpenScreen (my fault!)
  212. ;                           Added partial timer.device support (UNIT_VBLANK,UNIT_MICROHZ)
  213. ;                           Fake ReadEClock added, GETSYSTIME request added
  214. ;                           Fixed intuition.GetCurrentTime
  215. ;                           Added intuition.PubScreenStatus
  216. ;                           Added graphics.BestModeIDA
  217. ;                           Added font pointer in most intuition/graphics structures
  218. ;                           Added ciax.SetICR (Wings,lowlevel.library)
  219. ;                           Added lowlevel.StartTimerInt
  220. ;                           Added dummy nonvolatile.GetNVInfo
  221. ;                           Added lowlevel.QueryKeys
  222. ;                           Enlarged KeyboardMatrix data (could be out of bounds!)
  223. ;                           Allowed cd.device open again. Maybe trouble with Robocod CD32...
  224. ;                           Adjusted timers reload values, Fire&Ice CD sounds good (V1.27)
  225. ;    07.11.2000 Harry    Added RemTimerInt, SetICR, debugged lowlevel-
  226. ;                TimerInt-stuff, debugged cia.resource
  227. ;       11.11.2000 JOTD     Fixed CLI structure stack size (now in longwords)
  228. ;                           Added dma sprite enable on intuition open
  229. ;                           Protected FreeMem() against $bbbbbbbb address (Wings, temporary)
  230. ;                           Completed InitRastPort initialization (Test Drive II)
  231. ;                           Added partial RawDoFmt support
  232. ;                           Debugged OpenWindow/OpenScreen/active window/screen stuff
  233. ;                           Improved lowlevel.ReadJoyPort. Now handles multiple keypresses
  234. ;                           Changed joypad emulation keys: now F5 to F10
  235. ;                           Fixed second joystick button emulation (CD32 games)
  236. ;                           Finished to merge Harry's changes. Adjusted CIA timer rate (v1.28)
  237. ;       12.11.2000 JOTD     Added timer.ReadEClock and lowlevel.ElapsedTime,
  238. ;                           now Zool CD32 works (v1.29)
  239. ;       13.11.2000 Harry    Made Joypad-emulationkeys configurable, debugged
  240. ;                           lowlevel.library even more
  241. ;       17.11.2000 JOTD        Added console.RawKeyConvert (v1.30)
  242. ;       20.11.2000 Harry    Bugfixed intuition initialization and GetPrefs, added 
  243. ;                           graphics.BltMaskBitMapRastPort, fixed lowlevel.ReadJoyPort (v1.31)
  244. ;       22.11.2000 JOTD     Added exec.Cause (approximative, but works with Darkseed)
  245. ;                           Added intuition.UnlockIbase (dummy) (v1.32)
  246. ;       27.11.2000 JOTD     Ooops, fixed bug introduced some time ago: some games had
  247. ;                           no display (KQ6, Beneath a Steel Sky, Deuteros, and possibly others)
  248. ;                           Added dummy misc.resource (CAP) (v1.33)
  249. ;       01.12.2000 JOTD     Restored sprites in Deuteros/Millenium
  250. ;                           Ooops, merged both SetICR versions (mine & Harry) because
  251. ;                           of music slowness bug came back, sorry Harry! (v1.34)
  252. ;       15.12.2000 JOTD     graphics.ClipBlit added (Ports of Call)
  253. ;                           Fixed intuition.CloseWindow, now frees RastPort
  254. ;                           Improved intuition.OpenWindow
  255. ;                           Re-enabled Enqueue, added AddPort, RemPort, FindPort
  256. ;                           Added GetMsg, partial implementations of PutMsg & ReplyMsg
  257. ;                           Fixed graphics.UCopperListInit, rewritten graphics.MakeVPort
  258. ;                           Better graphics.MrgCop, can merge sprites/display/user clists
  259. ;                           Changed exec.FindName (some optimizers could lose Z flag)
  260. ;                           Implemented intuition.RethinkDisplay (Monkey Island)
  261. ;                           Replaced Add/Rem/Head/Tail+Remove by official exec macros
  262. ;                           (with register protection, as OSEmu uses them internally)
  263. ;                           Added intuition input handler to handle IDCMP events
  264. ;                           Bugfixed ss_num access in gfxlib sprite handling routines
  265. ;                           Better gfxlib sprite init: no more sprite glitches
  266. ;                           Did some hacks for Speris Legacy (intuition handler, sprites)
  267. ;                           Improved GetPrefs, now reads l/system-configuration if called
  268. ;                           Changed IND_ADDHANDLER in DoIo() for input.device
  269. ;                           Now Secret Of Monkey Island runs (still mouse problems)
  270. ;                           Added keyboard acknowledge at exec startup
  271. ;                           Restored old Harry's dos.Write code because bufferized one
  272. ;                           does not seem to work (v1.35)
  273. ;       22.12.2000 JOTD     fake egs.library open failure added (Tower Assault)
  274. ;                           Bugfixed graphics.FreeVPortCopLists (v1.36)
  275. ;       01.01.2001 JOTD     Ooops, incompatibility in old dos.Write routine, now fixed
  276. ;                           (v1.37)
  277. ;       06.01.2001 JOTD     Another regression fixed: Arcade Pool display was black.
  278. ;                           Ooops, another bug left in the old write routine, Damn!
  279. ;                           (v1.38)
  280. ;       10.01.2001 JOTD     Added dos.SetIoErr, dos.CheckSignal (dummy),
  281. ;                           Improved dos.IsInteractive
  282. ;       28.01.2001 JOTD     Changed intuition pointer management. Now only appears
  283. ;                           if SetPointer is called (v1.39)
  284. ;       30.01.2001 JOTD     Return value changed in graphics.Text (old expected value, LCP)
  285. ;       01.02.2001 JOTD     exec.AddTask first implementation try (not connected yet!!)
  286. ;                           added real exec.Forbid
  287. ;                           exec.Permit, private exec.Dispatch, exec.Switch, exec.Reschedule
  288. ;                           Slightly improved exec.CacheControl
  289. ;                           exec.AllocMem/FreeMem now task switching proof
  290. ;       17.02.2001 JOTD     Fixed 2nd joystick button problems with non-lowlevel lib games
  291. ;                           All DOS functions existing in kick 1.x also return result in D1
  292. ;                           (old BCPL expected behavior)
  293. ;                           Changed exec task type from NT_TASK to NT_PROCESS (more accurate)
  294. ;                           Duplicated fib_DirEntryType value in fib_EntryType, and changed
  295. ;                           directory type value. Also fixed protection flags (were inverted)
  296. ;                           Removed dirty direct ALLOCM/FREEM calls from other libs than exec
  297. ;                           Implemented real intuition.SetPrefs (v1.40)
  298. ;       22.02.2001 JOTD     dos.Open() always returned a filehandle even if file not found. Fixed
  299. ;                           Added error codes for dos.Open and dos.Lock
  300. ;                           exec.AllocMem fixed (no more fast -> try chip)
  301. ;                           Fixed intuition mouse event handling.
  302. ;                           Monkey Island finally works !! (v1.41)
  303. ;       23.02.2001 JOTD     Improved graphics.RastPort structure (added dummy Layer info)
  304. ;                           Fixed graphics.MakeVPort (Maniac Mansion)
  305. ;                           GetPrefs also looks in rootdir for system-configuration
  306. ;                           Fixed graphics.InitView (completely broken!)
  307. ;                           Renamed many dos,intuition,exec entrypoints for better
  308. ;                           coherence. Removed specific JST flags (were useless) (v1.42)
  309. ;       01.03.2001 Harry    Added real support for nonvolatile library (v1.43)                    
  310. ;       08.03.2001 JOTD     exec.AllocMem now avoid clearing memory if MEMB_CLEAR is unset
  311. ;                           (faster initialization)
  312. ;                           improved lowlevel.library keyboard interrupt routine
  313. ;                           dos.ParentDir added
  314. ;                           intuition.OpenScreenTagList fixed (v1.44)
  315. ;       08.03.2001 JOTD     Fixes due to non cleared memory allocation
  316. ;                           nonvolatile.GetNVList added (todo!)
  317. ;
  318. ;    Note:    - does not run with JumpingJackSon slave because 
  319. ;          it expects an old bug in dos.LoadSeg (APTR instead BPTR)
  320. ;
  321. ;  :Copyright.    GPL
  322. ;  :Language.    68000/68020 Assembler
  323. ;  :Translator.    Asm-One V1.30 R399, Barfly V2.9, Asm-Pro V1.12, Devpac V3.14
  324. ;---------------------------------------------------------------------------*
  325. ; Currently emulated functions:
  326. ;    exec.library
  327. ;        _LVOAddHead
  328. ;        _LVOAddIntServer
  329. ;        _LVOAddPort
  330. ;        _LVOAddTail
  331. ;        _LVOAllocAbs
  332. ;        _LVOAllocMem
  333. ;        _LVOAllocSignal
  334. ;        _LVOAvailMem
  335. ;        _LVOCloseDevice        ;dummy
  336. ;        _LVOCloseLibrary    ;dummy
  337. ;        _LVOCopyMem        ;not optimized
  338. ;        _LVODisable
  339. ;        _LVODoIO        ;some of trackdisk/input/keyboard
  340. ;        _LVOEnable
  341. ;        _LVOExitIntr        ;experimentally
  342. ;        _LVOFindName
  343. ;        _LVOFindTask        ;partially
  344. ;        _LVOForbid
  345. ;        _LVOFreeMem
  346. ;        _LVOGetMsg
  347. ;        _LVOInsert
  348. ;        _LVOOldOpenLibrary
  349. ;        _LVOOpenDevice        ;only trackdisk/input/keyboard/timer/console !
  350. ;        _LVOOpenLibrary
  351. ;        _LVOOpenResource
  352. ;        _LVOPermit        ;dummy
  353. ;        _LVORemove
  354. ;        _LVORemHead
  355. ;        _LVORemIntServer
  356. ;        _LVORemPort
  357. ;        _LVORemTail
  358. ;        _LVOSendIO
  359. ;        _LVOSetIntVector    ;exact v40, only audiointerrupts & blitter (but can be easily extended)
  360. ;        _LVOSetSignal
  361. ;        _LVOStackSwap        ;exact v40
  362. ;        _LVOSuperState        ;exact v39
  363. ;        _LVOSupervisor
  364. ;        _LVOUserState        ;exact v39
  365. ;    dos.library
  366. ;        _LVOClose
  367. ;        _LVOCurrentDir        ;returns always 0 for rootdir
  368. ;        _LVODeviceProc        ;dummy
  369. ;        _LVOCreateProc        ;of course no multitasking, but starts child
  370. ;        _LVODupLock        ;limited to files and null for rootdir
  371. ;        _LVOExamine        ;doesnt work for dirs atm, only files and volumes
  372. ;        _LVOInput        ;dummy
  373. ;        _LVOLoadSeg        ;can take advantage of fast memory if expansion on
  374. ;        _LVOLock        ;atm no dirs, only files and volumes
  375. ;        _LVOOpen
  376. ;        _LVOOutput        ;dummy
  377. ;        _LVORead
  378. ;        _LVOSeek
  379. ;        _LVOUnLoadSeg
  380. ;        _LVOUnLock        ;internal the same as dos.close
  381. ;        _LVOWaitForChar
  382. ;        _LVOWrite
  383. ;    graphics.library
  384. ;        _LVOAllocRaster
  385. ;        _LVOBltBitMap        ;has still some limitations !
  386. ;        _LVOBltClear        ;with CPU
  387. ;        _LVOBltTemplate        ;broken
  388. ;        _LVODisownBlitter    ;dummy
  389. ;        _LVOChangeSprite
  390. ;        _LVODraw        ;dummy !
  391. ;        _LVOFreeColorMap
  392. ;        _LVOFreeRaster
  393. ;        _LVOFreeSprite
  394. ;        _LVOGetColorMap
  395. ;        _LVOGetSprite
  396. ;        _LVOInitBitMap
  397. ;        _LVOInitRastPort
  398. ;        _LVOInitView        ;dummy !
  399. ;        _LVOInitVPort        ;dummy !
  400. ;        _LVOLoadView        ;sets copperlist
  401. ;        _LVOLoadRGB4        ;sets colors immediate
  402. ;        _LVOMakeVPort        ;builds copperlist
  403. ;        _LVOMove
  404. ;        _LVOMoveSprite
  405. ;        _LVOMrgCop
  406. ;        _LVOOwnBlitter        ;dummy
  407. ;        _LVOReadPixel
  408. ;        _LVOSetAPen
  409. ;        _LVOSetBPen
  410. ;        _LVOSetDrMd
  411. ;        _LVOSetRGB4        ;sets color immediate
  412. ;        _LVOUCopperListInit    ; untested
  413. ;        _LVOCMove
  414. ;        _LVOCWait
  415. ;        _LVOCBump
  416. ;        _LVOText
  417. ;        _LVOVBeamPos
  418. ;        _LVOWaitBlit
  419. ;        _LVOWaitTOF
  420. ;        _LVOWritePixel
  421. ;    intuition.library
  422. ;        _LVOClearDMRequest    ;dummy
  423. ;        _LVOCloseWindow
  424. ;        _LVOCloseWorkbench
  425. ;        _LVOOpenWindow
  426. ;        _LVOOpenScreen
  427. ;        _LVOSetPointer
  428. ;        _LVOClearPointer
  429. ;        _LVORethinkDisplay
  430. ;    ciaa.resource,ciab.resource
  431. ;        _LVOAddICRVector
  432. ;        _LVOAbleICR        ; untested, direct ROM rip (JOTD)
  433. ;        _LVORemICRVector
  434. ;    icon.library
  435. ;    layers.library
  436. ;    mathffp.library
  437. ;    mathtrans.library
  438. ;---------------------------------------------------------------------------*
  439. ; programs using or able to use this stuff (non exhaustive list):
  440. ;    AnotherWorld (WHDLoad), Arcade Pool (JST), Blue Angel 69 (WHDLoad), 
  441. ;       Bombuzal (WHDLoad), Bubble Bobble (JST), Castle Master (WHDLoad), 
  442. ;       Colorado, Crazy Cars 3 (WHDLoad), Deuteros (WHDLoad), Flashback (JST), 
  443. ;       Flink CD32 (WHDLoad), JumpingJackSon (WHDLoad), Lionheart (WHDLoad),
  444. ;       Metal Mutant, North&South (WHDLoad), Oscar CD32 (JST), 
  445. ;       PushOver (WHDLoad), Sensible Soccer CD32 (JST), Zynaps (JST),
  446. ;       Lemmings Holiday 94, Slayer (WHDLoad), 1943, Street Fighter II Turbo CD32
  447. ;       Shadow Fighter CD32 (JST), Son Of Stag series (JST), 
  448. ;       Street Fighter 1 (JST), Bubba and Stix (JST), Sierra Soccer (JST),
  449. ;       Clockwiser (JST), Morph CD32 (JST), Impossible Mission 2025 (JST),
  450. ;       Heimdall 2 AGA (JST), Soldier Of Light (WHDLoad), Cruise For A Corpse,
  451. ;       Operation Stealth (JST), Arcade Snooker (JST), Dune (JST), Heimdall 2 AGA (JST),
  452. ;       Tower Of Souls (JST), Panza Kick Boxing (WHDLoad), Legacy Of Sorasil (JST),
  453. ;       Super Obliteration (WHDLoad), Banshee (JST), Full Metal Planete (JST),
  454. ;       Zool 2 (JST), SlamTilt (JST), Bomb'X (JST), Frontier (JST), Robocod CD32,
  455. ;       MadTV, Fighting Spirit (JST), Guardian, Lost Vikings (JST), NightShift (JST),
  456. ;       Settlers (JST), Alien Breed SE CD32 (JST), Fire & Ice CD32 (JST),
  457. ;       Chaos Engine 2 (JST/WHDLoad), Zool CD32 (JST), Secret Of Monkey Island (JST),
  458. ;       Led Storm (JST)
  459. ;
  460. ; unfinished:
  461. ;    Millenium 2·2, Lords of War (works until title), 
  462. ;    Prehistorik (gfx.MrgCop must be improved),
  463. ;       Black Tiger (JST) (works until title)
  464. ;    Imperium (access fault), Wings (problems with overlay)
  465. ;---------------------------------------------------------------------------*
  466. ; ToDo:
  467. ;    rewriting memory management for less memory waste
  468. ;    implement dos.waitforchar fully: allow open to raw:, read characters,
  469. ;      close
  470. ;    support nonexisting files in dos.lib
  471. ;---------------------------------------------------------------------------*
  472. ; How to use in WHDLoad:
  473. ;
  474. ;    ;load the osemu module
  475. ;        lea    (_osemu,pc),a0        ;filename of the osemu module
  476. ;        lea    ($400.w),a1        ;the address on which you have
  477. ;                        ;assembled the osemu module
  478. ;        move.l    (_resload,pc),a2    ;the resload base
  479. ;        jsr    (resload_LoadFileDecrunch,a2)    ;this allows to
  480. ;                        ;compress the osemu
  481. ;    ;init the osemu module
  482. ;                        ;system stack has to be on 
  483. ;                        ;top of memory (automatically
  484. ;                        ;done by whdload)
  485. ;        move.l    (_resload,pc),a0    ;the resload base
  486. ;        lea    (_base,pc),a1        ;the slave structure
  487. ;        jsr    $400
  488. ;
  489. ;    ;start the program
  490. ;        move    #0,sr            ;if the program uses the os it
  491. ;                        ;should executed in user mode
  492. ;
  493. ;    ;if you want to add a trainer use this, to setup a routine which will
  494. ;    ;be called each time a key is pressed (d0.b contains rawkeycode)
  495. ;        lea    (_trainer,pc),a0
  496. ;        move.l    a0,($404.w)
  497. ;    ;if you want to use fast memory:
  498. ;        lea    (_base,pc),a1        ;the slave structure
  499. ;        move.l    (ws_ExpMem,A1),($424.w)
  500. ;        move.l    #<expmem size>,($428.w)
  501. ;
  502. ;---------------------------------------------------------------------------*
  503.  
  504. ;---------------------------------------------------------------------------*
  505. ; How to use in JST:
  506. ;
  507. ;    ;allocate expansion memory if needed
  508. ;
  509. ;    move.l    #$100000,D0    ; 1meg
  510. ;    JSRABS    AllocExtMem
  511. ;
  512. ;    ;load the osemu module (when the OS is active)
  513. ;
  514. ;    JSRABS    UseHarryOSEmu
  515. ;
  516. ;    ;start the program
  517. ;
  518. ;    GO_SUPERVISOR
  519. ;    SAVE_OSDATA    xxxx            ; classical OS-kill call
  520. ;
  521. ;    move    #0,sr            ;if the program uses the os it
  522. ;                    ;should executed in user mode
  523. ;                    ;but it's seldom necessary
  524. ;
  525. ;    ;if you want to add a trainer use this, to setup a routine which will
  526. ;    ;be called each time a key is pressed (d0.b contains rawkeycode)
  527. ;        lea    (_trainer,pc),a0
  528. ;        move.l    a0,$400+OSM_SLVTRAINER
  529. ;
  530. ;       Anyway, you should use the InstallWizard to create JST install template
  531. ;       OSEmu is fully supported by InstallWizard
  532. ;
  533. ;---------------------------------------------------------------------------*
  534.  
  535.     INCLUDE    whoami.i            ;this contains only a symbol
  536.  
  537.     IFD HARRY                ;(asm-one)
  538.         INCDIR    ASM-ONE:INCLUDE2.0/
  539.         INCLUDE LIBRARIES/DOS_LIB.I
  540.         INCLUDE LIBRARIES/DISKFONT_LIB.I
  541.         INCLUDE    LIBRARIES/DOS.I
  542.         INCLUDE    DEVICES/INPUT.I
  543.         INCLUDE    DEVICES/KEYBOARD.I
  544.         INCLUDE    DEVICES/TRACKDISK.I
  545.         INCLUDE    DOS/DOSEXTENS.I
  546.         INCLUDE    DOS/DOSHUNKS.i
  547.         INCLUDE RESOURCES/CIA_LIB.I
  548.         INCLUDE UTILITY/UTILITY_LIB.I
  549.         INCLUDE EXEC/EXEC_LIB.I
  550.         INCLUDE EXEC/LISTS.I
  551.         INCLUDE    EXEC/MEMORY.I
  552.         INCLUDE    EXEC/TASKS.I
  553.         INCLUDE    GRAPHICS/GRAPHICS_LIB.I
  554.         INCLUDE    GRAPHICS/LAYERS_LIB.I
  555.         INCLUDE    GRAPHICS/GFXBASE.I
  556.         INCLUDE    GRAPHICS/DISPLAYINFO.I
  557.         INCLUDE    GRAPHICS/SPRITE.I
  558.         INCLUDE    HARDWARE/CUSTOM.I
  559.         INCLUDE INTUITION/INTUITION_LIB.I
  560.         INCLUDE INTUITION/INTUITION.I
  561.         INCLUDE INTUITION/PREFERENCES.I
  562.         INCLUDE    MATH/MATHFFP_LIB.I
  563.         INCLUDE    MATH/MATHTRANS_LIB.I
  564.         INCLUDE    OWN/CCRMAKRO
  565.         INCLUDE    OWN/WHDLOAD.I
  566.         INCLUDE    WORKBENCH/ICON_LIB.I
  567.         INCLUDE    OWN/WHDMACROS.I
  568.         INCLUDE    OWN/LOWLEVEL_LIB.I
  569.         INCLUDE    OWN/LOWLEVEL.I
  570.         INCLUDE    OWN/NONVOLATILE_LIB.I
  571.         INCLUDE RESOURCES/DISK_LIB.I
  572.         INCLUDE LIBRARIES/LOCALE_LIB.I
  573.         INCLUDE DEVICES/TIMER_LIB.I
  574.  
  575. HUNKB_CHIP    =    30
  576. HUNKB_FAST    =    31
  577.         INCDIR    ASM-ONE:OWN/PATCHER/OSEMUnew/
  578.  
  579. JSRLIB MACRO
  580.     JSR     _LVO\1(a6)
  581.     ENDM
  582. JMPLIB MACRO
  583.     JMP     _LVO\1(a6)
  584.     ENDM                       
  585.     org    $400
  586.     load    $100000
  587.     ENDC
  588.     IFD WEPL                ;(barfly)
  589.         INCDIR    Includes:
  590.         INCLUDE    devices/input.i
  591.         INCLUDE    devices/inputevent.i
  592.         INCLUDE    devices/trackdisk.i
  593.         INCLUDE    devices/keyboard.I
  594.         INCLUDE    dos/dos.i
  595.         INCLUDE    dos/doshunks.i
  596.         INCLUDE    dos/dosextens.i
  597.         INCLUDE    exec/memory.i
  598.         INCLUDE    exec/tasks.i
  599.         INCLUDE exec/lists.i
  600.         INCLUDE    graphics/gfx.i
  601.         INCLUDE    graphics/gfxbase.i
  602.         INCLUDE    graphics/rastport.i
  603.         INCLUDE    graphics/sprite.i
  604.         INCLUDE    graphics/view.i
  605.         INCLUDE    intuition/intuition.i
  606.         INCLUDE    intuition/preferences.i
  607.         INCLUDE    lvo/cia.i
  608.         INCLUDE    lvo/dos.i
  609.         INCLUDE    lvo/exec.i
  610.         INCLUDE    lvo/graphics.i
  611.         INCLUDE    lvo/icon.i
  612.         INCLUDE    lvo/intuition.i
  613.         INCLUDE    lvo/layers.i
  614.         INCLUDE    lvo/mathffp.i
  615.         INCLUDE    lvo/mathtrans.i
  616.         INCLUDE    whdload.i
  617.         INCLUDE    whdmacros.i    ; added for WHDLoad v10 includes
  618.  
  619.         IFD BARFLY
  620.         BOPT    O+ OG+            ;enable optimizing
  621.         BOPT    ODd- ODe-        ;disable mul optimizing
  622.         BOPT    w4-            ;disable 64k warnings
  623.         SUPER                ;disable supervisor warnings
  624.         ENDC
  625.  
  626.         ORG    $400
  627.         OUTPUT    OSEmu.400
  628.         ;OUTPUT    wart:a/anotherworld/osemumodule400.bin
  629.         ;OUTPUT    wart:b/bombuzal/osemumodule400.bin
  630.         ;OUTPUT    wart:d-f/deuteros/OSEmu.400
  631.         ;OUTPUT    wart:h-j/jumpingjackson/osemumodule400.bin
  632.         ;OUTPUT    wart:m/millennium2·2/OSEmu.400
  633.         ;OUTPUT    wart:n-p/north&south/OSEmu.400
  634.     ENDC
  635.  
  636.     IFD JOTD                ;(phxass/barfly)
  637.         INCDIR    include:
  638.         INCLUDE    "LVOs.i"
  639.         INCLUDE    "jst_libs.i"
  640.         INCLUDE    "dos/doshunks.i"
  641.         INCLUDE "devices/trackdisk.i"
  642.         INCLUDE "devices/input.i"
  643.         INCLUDE "exec/io.i"
  644.         INCLUDE    "devices/keyboard.I"
  645.         INCLUDE    "devices/timer.i"
  646.         INCLUDE "devices/inputevent.i"
  647.         INCLUDE "exec/interrupts.i"
  648.         INCLUDE "exec/ports.i"
  649.         INCLUDE "exec/lists.i"
  650.         INCLUDE    "graphics/gfx.i"
  651.         INCLUDE    "graphics/gfxbase.i"
  652.         INCLUDE    "graphics/rastport.i"
  653.         INCLUDE    "graphics/copper.i"
  654.         INCLUDE    "graphics/sprite.i"
  655.         INCLUDE    "graphics/view.i"
  656.         INCLUDE    "intuition/intuition.i"
  657.         INCLUDE    "intuition/preferences.i"
  658.         INCLUDE    "intuition/screens.i"
  659.         INCLUDE    "libraries/lowlevel.i"
  660.         INCLUDE    "utility/utility.i"
  661.         INCLUDE    "libraries/nonvolatile.i"
  662.  
  663.         INCLUDE    "whdload.i"
  664.         INCLUDE    "whdmacros.i"    ; added for WHDLoad v10 includes
  665.  
  666.         IFD BARFLY
  667.         BOPT    O+ OG+            ;enable optimizing
  668.         BOPT    ODd- ODe-        ;disable mul optimizing
  669.         BOPT    w4-            ;disable 64k warnings
  670.         BOPT    wo-            ;disable optimizer warnings
  671.         SUPER                ;disable supervisor warnings
  672.         ENDC
  673.  
  674.         ORG    $400
  675.         OUTPUT    C:OSEmu.400
  676.  
  677.     ENDC
  678.  
  679.     IFD MR.LARMER                ;(devpac)
  680.         INCDIR    Include:
  681.         INCLUDE    devices/input.i
  682.         INCLUDE    devices/inputevent.i
  683.         INCLUDE    devices/keyboard.i
  684.         INCLUDE    devices/trackdisk.i
  685.         INCLUDE    dos/dos.i
  686.         INCLUDE    dos/doshunks.i
  687.         INCLUDE    dos/dosextens.i
  688.         INCLUDE    exec/memory.i
  689.         INCLUDE    graphics/gfx.i
  690.         INCLUDE    graphics/gfxbase.i
  691.         INCLUDE    graphics/rastport.i
  692.         INCLUDE    graphics/sprite.i
  693.         INCLUDE    graphics/view.i
  694.         INCLUDE    intuition/intuition.i
  695.         INCLUDE    intuition/preferences.i
  696.         INCLUDE    utility/utility.i
  697.         INCLUDE    libraries/lowlevel.i
  698.         INCLUDE    libraries/nonvolatile.i
  699.         INCLUDE    lvo/cia_lib.i
  700.         INCLUDE    lvo/dos_lib.i
  701.         INCLUDE    lvo/exec_lib.i
  702.         INCLUDE    lvo/graphics_lib.i
  703.         INCLUDE    lvo/icon_lib.i
  704.         INCLUDE    lvo/intuition_lib.i
  705.         INCLUDE    lvo/layers_lib.i
  706.         INCLUDE    lvo/lowlevel_lib.i
  707.         INCLUDE    lvo/utility_lib.i
  708.         INCLUDE    lvo/mathffp_lib.i
  709.         INCLUDE    lvo/mathtrans_lib.i
  710. ;        INCLUDE    lvo/nonvolatile_lib.i
  711.         INCLUDE    whdload.i
  712.  
  713.         OPT    O+ OG+            ;enable optimizing
  714.         OPT    P=68020
  715.  
  716.         ORG    $400
  717.         OUTPUT    OSEmu.400
  718.     ENDC
  719.  
  720. USPLENGTH=$2000        ;reserved area for USP, enlarge if necessary
  721.  
  722. CORRECTDEVICES=1    ;if set 0, module will be assembled with
  723.             ;cut-down devices (no list connection, no
  724.             ;msgport of td.dev, no library-like device-
  725.             ;structure)
  726.  
  727. ;;BIG_OSEMU = 1        ;if set, OSEmu will be bigger, but richer
  728.  
  729. ; macros copied from system includes, but improved for local labels support
  730.  
  731. DISABLE:MACRO
  732.           MOVE.W  #INTF_INTEN,_custom+intena    ;(NOT IF_SETCLR)+IF_INTEN
  733.           ADDQ.B  #1,IDNestCnt(A6)
  734.         ENDM
  735.  
  736. ENABLE:MACRO
  737.           SUBQ.B  #1,IDNestCnt(A6)
  738.           BGE.S   .ENABLE\@
  739.           MOVE.W  #INTF_SETCLR!INTF_INTEN,_custom+intena    ;IF_SETCLR+IF_INTEN
  740. .ENABLE\@:
  741.     ENDM
  742.  
  743. ; cleaner way to fix illegal memory constants
  744.  
  745. VAL_DDDDDDDD = $DDDDDDDD
  746. VAL_EEEEEEEE = $EEEEEEEE
  747.  
  748. ;---------------------------------------------------------------------------*
  749. **************************************************************************
  750. *   INITIALIZATION CALL                                                  *
  751. **************************************************************************
  752.  
  753.         bra.w    _Init
  754.         CNOP 0,4    ;essential for correct OSEmu-Structure !
  755.  
  756. **************************************************************************
  757. *   GLOBAL VARIABLES                                                     *
  758. **************************************************************************
  759.  
  760. ;-------------------------------------------------------------------------
  761. ; the following data are part of the OSEmu structure, the offsets of these
  762. ; data are guaranteed to remain unchanged for all future versions
  763.  
  764. VERSION = 1
  765. RELEASE = 45    ; to increase each time the binary is delivered
  766.  
  767. OSM_SLVTRAINER    DC.L    0    ;OSEmu-Offset = 4
  768. OSM_LASTLOADSEG    dc.l    0    ;OSEmu-Offset = 8
  769. OSM_ID        dc.b    "OSEM"    ;OSEmu-Offset = 12 : added by JOTD
  770. OSM_VER        dc.w    VERSION    ;OSEmu-Offset = 16 : ""
  771.         dc.w    RELEASE    ;OSEmu-Offset = 18 : ""
  772.         dc.l    0    ;OSEmu-Offset = 20 : vacant
  773. OSM_ICONIFYCODE    dc.l    0    ;OSEmu-Offset = 24 : ""
  774. OSM_ICONIFYKEY    dc.l    0    ;OSEmu-Offset = 28 : ""
  775. OSM_UNUSED    dc.l    0    ;OSEmu-Offset = 32 : ""
  776. OSM_EXPMEM    dc.l    0    ;OSEmu-Offset = 36 : ""
  777. OSM_EXPSIZE    dc.l    0    ;OSEmu-Offset = 40 : ""
  778. OSM_DEBUGENTRY    dc.l    0    ;OSEmu-Offset = 44 : ""
  779. OSM_JOYPAD1KEYS    dc.w    $5455,$5657,$5859    ;keys F5-F10 for pad1, offs.48
  780. OSM_JOYPAD0KEYS    dc.w    $0506,$0708,$090a    ;keys 5-0 for pad0, offset 54
  781. OSM_COPLIST1    dc.l    0            ; offset 58 system copper list 1 value
  782. OSM_COPLIST2    dc.l    0            ; offset 62 system copper list 2 value
  783. OSM_WRITE_BUFSIZE    dc.l    DEFAULT_WRITE_BUFSIZE        ; offset 66 default dos.Write buffer
  784. OSM_PORTS_CONFIG
  785.         dc.b    JP_TYPE_GAMECTLR,JP_TYPE_GAMECTLR    ; 2 joypads connected
  786.         dc.l    0,0,0,0,0,0,0    ; future use
  787.  
  788.  
  789. **************************************************************************
  790. *   VERSION                                                              *
  791. **************************************************************************
  792.  
  793.     IFND BARFLY
  794.         DC.B    0,"$VER: OS_EMUMODULE ",VERSION+'0',".",RELEASE/10+'0',(RELEASE-(RELEASE/10)*10)+'0'," (20/11/00)",0
  795.     ELSE
  796.         dc.b    "$VER: OSEmu ",VERSION+'0',".",RELEASE/10+'0',(RELEASE-(RELEASE/10)*10)+'0'," "
  797.         DOSCMD    "WDate >T:date"
  798.         INCBIN    "T:date"
  799.         dc.b    " by Harry/MrLarmer/WEPL/JOTD",10,0
  800.         dc.b    "$Id: osemu.asm 1.1 2000/20/05 04:10:48 jotd Exp jotd $"
  801.     ENDC
  802.     EVEN
  803.  
  804. ;-------------------------------------------------------------------------
  805.  
  806. ALLOCMTABSIZE    DC.L    0        ; chip memory
  807. ALLOCFASTMTABSIZE    DC.L    0    ; fast memory
  808. _Slave        dc.l    0
  809. _RESLOAD    dc.l    0
  810. _dosbase    dc.l    0
  811. _gfxbase    dc.l    0
  812. _intbase    dc.l    0
  813. _ilibbase    dc.l    0
  814. _laybase    dc.l    0
  815. _mffpbase    dc.l    0
  816. _mtrbase    dc.l    0    ; added by JOTD
  817. _lowlbase    dc.l    0    ; added by JOTD
  818. _utilbase    dc.l    0    ; added by JOTD
  819. _keymbase    dc.l    0    ; added by JOTD
  820. _loclbase    dc.l    0    ; added by JOTD
  821. _dskfbase    dc.l    0    ; added by JOTD
  822. _franbase    dc.l    0    ; added by JOTD
  823. _nonvbase    dc.l    0    ; added by JOTD
  824. _diskbase    dc.l    0    ; added by JOTD
  825. _miscbase    dc.l    0    ; added by JOTD
  826. _ciaabase    dc.l    0
  827. _ciabbase    dc.l    0
  828. _inputhandler    dc.l    0            ;input.device handler (interrupt server structure)
  829. _inputevent    ds.b    ie_SIZEOF
  830. _tags        dc.l    WHDLTAG_ATTNFLAGS_GET
  831.         dc.w    0
  832. _attnflags    dc.w    0
  833.         dc.l    WHDLTAG_ECLOCKFREQ_GET
  834. _eclockfreq    dc.l    0
  835.         dc.l    WHDLTAG_MONITOR_GET
  836. _monitor    dc.l    0
  837.         dc.l    WHDLTAG_CHIPREVBITS_GET
  838. _chiprev    dc.l    0
  839.         dc.l    WHDLTAG_Private3
  840. _p3        dc.l    0
  841.         dc.l    0
  842.  
  843. _libtable    dc.l    _dosname,DOSINIT
  844.         dc.l    _gfxname,GFXINIT
  845.         dc.l    _intname,INTUIINIT
  846.         dc.l    _execname,EXEC2INIT
  847.         dc.l    _ilibname,ILIBINIT
  848.         dc.l    _layname,LAYERSINIT
  849.         dc.l    _lowlname,LOWLINIT
  850.         dc.l    _franname,FRANINIT
  851.         dc.l    _nonvname,NONVINIT
  852.         dc.l    _egsname,EGSINIT
  853.  
  854.         IFD    BIG_OSEMU
  855.         dc.l    _mtrname,MATHTRANSINIT
  856.         ENDC
  857.         dc.l    _mffpname,MATHFFPINIT
  858.         dc.l    _utilname,UTILINIT
  859.         dc.l    _keymname,KEYMINIT
  860.         dc.l    _loclname,LOCLINIT
  861.         dc.l    _dskfname,DSKFINIT
  862.  
  863.         dc.l    0
  864. _restable    dc.l    _ciaaname,CIAAINIT
  865.         dc.l    _ciabname,CIABINIT
  866.         dc.l    _diskname,DISKINIT
  867.         dc.l    _miscname,MISCINIT
  868.         dc.l    0
  869.  
  870. _devtable
  871. _tddevtable    DC.L    _tdname,0
  872. _inpdevtable    DC.L    _inpname,0
  873. _auddevtable    DC.L    _audname,0
  874. _cddevtable    DC.L    _cdname,0
  875. _cdtvdevtable    DC.L    _cdtvname,0
  876. _kbdevtable    DC.L    _kbdevname,0
  877. _condevtable    DC.L    _condevname,0
  878. _timerdevtable    DC.L    _timerdevname,0
  879.         DC.L    0
  880.  
  881. ; added by JOTD
  882. sec_timer:
  883.     dc.l    $295A6D00    ; in year 2000, sometime
  884. microsec_timer:
  885.     dc.l    0
  886. microsec_timer_vbl:
  887.     dc.l    0
  888. microsec_vbl_interval:
  889.     dc.l    0
  890. potinp_value:
  891.     dc.w    $FFFF
  892.  
  893. _last_joy0dat    dc.w    0
  894.         cnop    0,4
  895.  
  896. _sprites    dc.l    -1,-1,-1,-1,-1,-1,-1,-1
  897.  
  898.     even
  899. KBDQUALIFIER    DC.B    0
  900.         DC.B    0
  901.  
  902. _last_lmb    dc.b    0
  903. _last_rmb    dc.b    0
  904.  
  905. _mffpname    dc.b    'mathffp.library',0
  906. _layname    dc.b    'layers.library',0
  907. _ilibname    dc.b    'icon.library',0
  908. _execname    dc.b    "exec.library",0
  909. _dosname    dc.b    "dos.library",0
  910. _gfxname    dc.b    "graphics.library",0
  911. _intname    dc.b    "intuition.library",0
  912. _lowlname    dc.b    "lowlevel.library",0    ; added by JOTD
  913. _utilname    dc.b    "utility.library",0    ; added by JOTD
  914. _dskfname    dc.b    "diskfont.library",0    ; added by JOTD
  915. _keymname    dc.b    "keymap.library",0    ; added by JOTD
  916. _loclname    dc.b    "locale.library",0    ; added by JOTD
  917. _franname    dc.b    "freeanim.library",0    ; added by JOTD
  918. _nonvname    dc.b    "nonvolatile.library",0    ; added by JOTD
  919. _mtrname    dc.b    "mathtrans.library",0    ; added by JOTD
  920. _egsname    dc.b    'egs.library',0
  921. _ciaaname    dc.b    "ciaa.resource",0
  922. _ciabname    dc.b    "ciab.resource",0
  923. _diskname    dc.b    "disk.resource",0
  924. _miscname    dc.b    "misc.resource",0
  925.     EVEN
  926. _tdname        dc.b    'trackdisk.device',0
  927.     EVEN
  928. _inpname    dc.b    'input.device',0
  929.     EVEN
  930. _audname    dc.b    'audio.device',0
  931.     EVEN
  932. _cdname        dc.b    'cd.device',0
  933.     EVEN
  934. _cdtvname    dc.b    'cdtv.device',0
  935.     EVEN
  936. _kbdevname    dc.b    'keyboard.device',0
  937.     EVEN
  938. _condevname    dc.b    'console.device',0
  939.     EVEN
  940. _timerdevname    dc.b    'timer.device',0
  941.  
  942.     CNOP    0,4
  943.  
  944. ;task gets a process structure
  945.  
  946. _EXECLIBTASK    DC.L    0
  947.         DC.L    0
  948.         DC.B    NT_PROCESS
  949.         DC.B    0
  950.         DC.L    _execname
  951.         DC.B    0
  952.         DC.B    TS_RUN
  953.         DC.B    0
  954.         DC.B    0
  955.         DC.L    0        ;TC_SIGALLOC
  956.         DC.L    0
  957.         DC.L    0
  958.         DC.L    0
  959.         DC.W    0
  960.         DC.W    0
  961.         DC.L    0
  962.         DC.L    0
  963.         DC.L    0
  964.         DC.L    0
  965.         DC.L    0
  966.         DC.L    0
  967.         DC.L    0
  968.         DC.L    0
  969.         DC.L    0    ; tc_UserData
  970.  
  971.         DC.L    0
  972.         DC.L    0
  973.         DC.L    0
  974.         DC.B    0
  975.         DC.B    0
  976.  
  977.         DC.L    0
  978.  
  979.         ; end of task, start of process fields
  980.  
  981. _EXECLIBMSGPORT    DC.L    0
  982.         DC.L    0
  983.         DC.B    NT_MSGPORT
  984.         DC.B    0
  985.         DC.L    0
  986.  
  987.         DC.B    PA_SIGNAL
  988.         DC.B    0        ;BIT 0 AS SIGNALBIT
  989.         DC.L    _EXECLIBTASK
  990.  
  991.         DC.L    0        ;NO MESSAGES YET
  992.         DC.L    0
  993.         DC.L    0
  994.         DC.B    NT_MESSAGE
  995.         DC.B    0
  996.  
  997.  
  998.         DC.W    0
  999.  
  1000. _bcplcorrect3
  1001.         DC.L    _EXECSEGLIST
  1002.         DC.L    USPLENGTH-$20    ;StackSize
  1003.         DC.L    $EEEEEEEC    ;INVALID GLOBVEC
  1004.         DC.L    1        ;task number=1
  1005.         DC.L    0
  1006.         DC.L    0
  1007.  
  1008.         DC.L    0    ;CurrentDir
  1009.         DC.L    0            ; Current Input Stream
  1010.         DC.L    OUTPUT_HANDLER_MAGIC    ; Current Output Stream (JOTD)
  1011.         DC.L    VAL_EEEEEEEE
  1012.         DC.L    VAL_EEEEEEEE
  1013. _bcplcorrect1    DC.L    _CLI        ;pointer on CLI structure
  1014.         DC.L    VAL_EEEEEEEE
  1015.         DC.L    0
  1016.         DC.L    -1
  1017. ;disabled, only for Kick 2.x and higher
  1018. ;        DC.L    VAL_EEEEEEEE    ;homedir
  1019. ;        DC.L    0        ;flags
  1020. ;        DC.L    0        ;exitcode
  1021. ;        DC.L    VAL_EEEEEEEE    ;exitdata
  1022. _EXECLIBPROCESS_SIZEOF
  1023.  
  1024.     IFNE    _EXECLIBMSGPORT-_EXECLIBTASK-$5C
  1025.     FAIL
  1026.     ENDC
  1027.  
  1028.     IFNE    _EXECLIBPROCESS_SIZEOF-_EXECLIBTASK-$BC
  1029.     FAIL
  1030.     ENDC
  1031.  
  1032. _DOSLIST
  1033.     dc.l    0
  1034.     dc.l    DLT_VOLUME
  1035.     dc.l    _EXECLIBMSGPORT
  1036.     dc.l    $EEEEEEE3
  1037.     dc.l    $EEEEEEE0
  1038.     dc.l    $EEEEEEE1
  1039.     dc.l    $EEEEEEE2
  1040.     dc.l    $EEEEEEE3
  1041.     dc.l    $EEEEEEE0
  1042.     dc.l    $EEEEEEE1
  1043.     dc.l    $EEEEEEE2
  1044.     dc.l    $EEEEEEE3
  1045.     dc.l    $EEEEEEE0
  1046.     dc.l    $EEEEEEE1
  1047.     dc.l    $EEEEEEE2
  1048.     dc.l    $EEEEEEE3
  1049.     
  1050.     CNOP    0,4    ; must be longword aligned, because pointed by a BCPL pointer
  1051.  
  1052. _EXECSEGLIST:
  1053.     dc.l    $EEEEEEE0
  1054.     dc.l    $EEEEEEE1
  1055.     dc.l    $EEEEEEE2    ; 3 above are ROM segments, in the real life
  1056. LastSegList2:
  1057.     dc.l    $EEEEEEE3    ; our seglist
  1058.  
  1059.     CNOP    0,4    ; must be longword aligned, because pointed by a BCPL pointer
  1060.  
  1061. _CLI        DC.L    0
  1062.         DC.L    $EEEEE1E/4    ; /4 because if accessed it's in BCPL
  1063.         DC.L    $EEEEEE2
  1064.         DC.L    0        ; returncode
  1065. _bcplcorrect2    DC.L    _BCPL_CommandName
  1066.         DC.L    20
  1067.         DC.L    $EEEEEE3
  1068.         DC.L    $EEEEEE4
  1069.         DC.L    $EEEEEE5
  1070.         DC.L    $EEEEEE6
  1071.         DC.L    0
  1072.         DC.L    0
  1073.         DC.L    $EEEEEE7
  1074.         DC.L    (USPLENGTH-$20)/4    ; default stack (in longwords)
  1075.         DC.L    $EEEEEE8
  1076. LastSegList:
  1077.         DC.L    $EEEEEE9
  1078.  
  1079.     CNOP    0,4    ; BCPL pointer
  1080. _BCPL_CommandName
  1081.         blk.b    $40,0    ; was 8,'df0:anim',0, but needed to be changed by LoadSeg()
  1082.  
  1083.     EVEN
  1084.  
  1085. _EXECMINSIZE
  1086.  
  1087.     DS.B    822        ;-_LVOExecReserved08
  1088.  
  1089. INITLIST:MACRO
  1090.     move.l    A1,LH_TAILPRED(A1)    ; tailpred = head (empty list)
  1091.     addq.l    #4,A1
  1092.     clr.l    (A1)        ; tail = NULL
  1093.     move.l    A1,-(A1)    ; head points on tail
  1094.     ENDM
  1095.  
  1096. INTSTRUCT:MACRO
  1097.     dc.l    VAL_DDDDDDDD
  1098.     dc.l    VAL_DDDDDDDD
  1099.     dc.l    VAL_DDDDDDDD
  1100.     ENDM
  1101. SOFTINT:MACRO
  1102.     dc.l    0,0,0,0
  1103.     ENDM
  1104.  
  1105. _EXECLIBBASE
  1106.     DC.L    _LIBLIST+4
  1107.     DC.L    _LIBLIST
  1108.     DC.B    NT_LIBRARY
  1109.     DC.B    0
  1110.     DC.L    _execname
  1111.                     ;end of node structure
  1112.     DC.B    4
  1113.     DC.B    $EE
  1114.     DC.W    -_LVOCopyMemQuick
  1115.     DC.W    $24C
  1116.     DC.W    $28            ;version of exec.lib=KS3.1
  1117.     DC.W    $0A            ;subversion
  1118.     DC.L    VAL_EEEEEEEE
  1119.     DC.L    VAL_EEEEEEEE
  1120.     DC.W    0
  1121.                     ;end of library structure
  1122.     DC.W    $44            ;version: KS3.1
  1123.     DC.W    $EEEE            ;checksum
  1124.     DC.L    VAL_EEEEEEEE        ;ChkBase
  1125.     DC.L    VAL_EEEEEEEE
  1126.     DC.L    VAL_EEEEEEEE
  1127.     DC.L    VAL_EEEEEEEE
  1128.     DC.L    0
  1129.     DC.L    0
  1130.     DC.L    0
  1131.     DC.L    0
  1132.     DC.L    0
  1133.     DC.L    VAL_EEEEEEEE
  1134.     DC.L    0
  1135.     DC.W    $EEEE            ;ChkSum
  1136.  
  1137.     ; interrupt structures
  1138.  
  1139.     INTSTRUCT
  1140.     INTSTRUCT
  1141.     INTSTRUCT
  1142.     INTSTRUCT
  1143.     INTSTRUCT
  1144.     INTSTRUCT
  1145.     INTSTRUCT
  1146.     INTSTRUCT
  1147.     INTSTRUCT
  1148.     INTSTRUCT
  1149.     INTSTRUCT
  1150.     INTSTRUCT
  1151.     INTSTRUCT
  1152.     INTSTRUCT
  1153.     INTSTRUCT
  1154.     INTSTRUCT
  1155.  
  1156.     DC.L    _EXECLIBTASK    ; ThisTask offset
  1157.     DC.L    VAL_EEEEEEEE    ; IdleCount
  1158.     DC.L    VAL_EEEEEEEE    ; DispCount
  1159.     DC.W    $EEEE        ; Quantum
  1160.     DC.W    $DDDD        ; Elapsed
  1161.     DC.W    $EEEE        ; SysFlags
  1162.     DC.B    -1        ; IdNestCnt
  1163.     DC.B    -1        ; TdNestCnt
  1164.     DC.W    0        ; AttnFlags
  1165.     DC.W    0        ; AttnResched
  1166.     DC.L    VAL_EEEEEEEE
  1167.     DC.L    VAL_EEEEEEEE
  1168.     DC.L    VAL_EEEEEEEE
  1169.     DC.L    VAL_EEEEEEEE
  1170.     DC.L    VAL_EEEEEEEE        ;DUMMY, NORMALLY ALLOCATED SIGNALS
  1171.     DC.W    $EEEE
  1172.  
  1173.     DC.L    VAL_EEEEEEEE
  1174.     DC.L    VAL_EEEEEEEE
  1175.     DC.L    VAL_EEEEEEEE
  1176.     DC.W    $EEEE
  1177.  
  1178.     DC.L    VAL_EEEEEEEE
  1179.     DC.L    VAL_EEEEEEEE
  1180.     DC.L    VAL_EEEEEEEE
  1181.     DC.W    $EEEE
  1182.  
  1183.     IFNE    CORRECTDEVICES
  1184. _DEVLISTC    DC.L    _DEVLISTC+4
  1185.         DC.L    0
  1186.         DC.L    _DEVLISTC
  1187.         DC.B    NT_DEVICE
  1188.         DC.B    0
  1189.     ELSE
  1190.         DC.L    VAL_EEEEEEEE        ;DEVICELIST
  1191.         DC.L    VAL_EEEEEEEE
  1192.         DC.L    VAL_EEEEEEEE
  1193.         DC.W    $EEEE
  1194.     ENDC
  1195.  
  1196.     DC.L    VAL_EEEEEEEE
  1197.     DC.L    VAL_EEEEEEEE
  1198.     DC.L    VAL_EEEEEEEE
  1199.     DC.W    $EEEE
  1200.  
  1201. _LIBLIST    DC.L    _EXECLIBBASE
  1202.         DC.L    0
  1203.         DC.L    _EXECLIBBASE
  1204.         DC.B    NT_LIBRARY
  1205.         DC.B    0
  1206.  
  1207.         ; lists, empty at start
  1208.  
  1209. _PORTSLIST    DC.L    _PORTSLIST+4    ; added by JOTD
  1210.         DC.L    0
  1211.         DC.L    _PORTSLIST
  1212.         DC.B    NT_MSGPORT
  1213.         DC.B    0
  1214.  
  1215. _TASKREADYLIST    DC.L    _TASKREADYLIST+4
  1216.         DC.L    0
  1217.         DC.L    _TASKREADYLIST
  1218.         DC.B    NT_TASK
  1219.         DC.B    0
  1220.  
  1221. _TASKWAITLIST    DC.L    _TASKWAITLIST+4    ; added by JOTD
  1222.         DC.L    0
  1223.         DC.L    _TASKWAITLIST
  1224.         DC.B    NT_TASK
  1225.         DC.B    0
  1226.  
  1227. _SOFTINT0
  1228.     SOFTINT
  1229.     SOFTINT
  1230.     SOFTINT
  1231.     SOFTINT
  1232.     SOFTINT
  1233.  
  1234.     ; last alert
  1235.  
  1236.     DC.L    VAL_EEEEEEEE
  1237.     DC.L    VAL_EEEEEEEE
  1238.     DC.L    VAL_EEEEEEEE
  1239.     DC.L    VAL_EEEEEEEE
  1240.     
  1241.     DC.B    $EE            ;VBLANK FREQU
  1242.     DC.B    $EE            ;POWER SUPPLY FREQU
  1243.     
  1244.     DC.L    VAL_EEEEEEEE
  1245.     DC.L    VAL_EEEEEEEE
  1246.     DC.L    VAL_EEEEEEEE
  1247.     DC.W    $EEEE
  1248.  
  1249.     DC.L    VAL_EEEEEEEE
  1250.     DC.L    VAL_EEEEEEEE
  1251.     DC.W    $EEEE
  1252.  
  1253.     DS.B    12
  1254.     DS.B    20
  1255.     DS.B    $2C
  1256. _EMAXSIZE
  1257.  
  1258.     IFNE    _EMAXSIZE-_EXECLIBBASE-$278
  1259.     FAIL
  1260.     ENDC
  1261.  
  1262.  
  1263.     IFNE    CORRECTDEVICES
  1264. _TDDMSGPORT
  1265.     DC.L    0
  1266.     DC.L    0
  1267.     DC.B    NT_MSGPORT
  1268.     DC.B    0
  1269.     DC.L    _tdname
  1270.  
  1271.     dc.b    0
  1272.     dc.b    0
  1273.     DC.L    _EXECLIBTASK
  1274.  
  1275.     DC.L    0
  1276.     DC.L    0
  1277.     DC.L    0
  1278.     DC.B    NT_MESSAGE
  1279.     DC.B    0
  1280.  
  1281.     DC.L    1            ;DUE LORDS OF WAR -UNDOCUMENTED-
  1282.  
  1283.     ENDC
  1284.  
  1285.     cnop    0,4
  1286. _DummyCList:                ; added by JOTD
  1287.     dc.l    $FFFFFFFE
  1288.  
  1289. _Multitasks:
  1290.     dc.w    0
  1291.  
  1292. **************************************************************************
  1293. *   GLOBAL INITIALIZATION                                                *
  1294. **************************************************************************
  1295. ;REQUIRES NOW AT LEAST WHDLOAD 7.0
  1296. ;ALTHOUGH THERE IS NO MULTITASKING IMPLEMENTED AT THE MOMENT,
  1297. ;ALL FUNCTIONS MUST BE IMPLEMENTED REENTRANTLY
  1298. ;
  1299. ; IN:    A0 = resload base
  1300. ;    A1 = pointer to slave structure
  1301. ; OUT:    A1 = ioreq of trackdisk.device
  1302. ;    A6 = execbase
  1303.  
  1304. _Init        movem.l    d0-a0/a2-a5,-(a7)
  1305.  
  1306.         move.l    $4.W,a6
  1307.  
  1308.         cmp.w    #7,(ws_Version,a1)    ;minimum version
  1309.         blo    _ill
  1310.  
  1311.         move.l    a0,_RESLOAD
  1312.         move.l    a1,(_Slave)
  1313.  
  1314.     ;install a default copperlist
  1315.  
  1316.         move.l    #_DummyCList,(_custom+cop1lc)
  1317.  
  1318.     ;correct structures
  1319. BCPLCORRECT    MACRO
  1320.         move.l    \1,d0
  1321.         lsr.l    #2,d0
  1322.         move.l    d0,\1
  1323.         ENDM
  1324.         BCPLCORRECT    _bcplcorrect1
  1325.         BCPLCORRECT    _bcplcorrect2
  1326.         BCPLCORRECT    _bcplcorrect3
  1327.         BCPLCORRECT    _bcplcorrect6
  1328.         BCPLCORRECT    _bcplcorrect7
  1329.         BCPLCORRECT    _bcplcorrect8
  1330.  
  1331.     ;init memory managment
  1332.  
  1333.     ; chip memory
  1334.  
  1335.         lea    _osemu_end,a0        ;FIRST CLEAR WHOLE TABLE (MARK MEM AS USED)
  1336.         move.l    (ws_BaseMemSize,a1),d0
  1337.         SUBQ.L    #8,D0
  1338.         LSR.L    #3,D0            ;OK, AMOUNT OF BITS TO CLEAR IS IN D0
  1339.         LSR.L    #3,D0            ;AMOUNT OF BYTES TO CLEAR IN D0
  1340. .1        CLR.B    (A0)+
  1341.         SUBQ.L    #1,D0
  1342.         BPL.S    .1
  1343.         MOVE.L    A0,ALLOCMTABSIZE
  1344.         move.l    (ws_BaseMemSize,a1),d0
  1345.         SUB.L    A0,D0
  1346.         SUB.L    #$1000,D0        ;$1000 SYSTEM STACK ON TOP
  1347.         MOVE.L    A0,A1            ;FREE APPROPRIATE SIZE
  1348.         BSR    _FreeMem
  1349.  
  1350.     ; fast memory if available
  1351.  
  1352.         move.l    OSM_EXPMEM,a0        ;FIRST CLEAR WHOLE TABLE (MARK MEM AS USED)
  1353.         cmp.l    #0,A0
  1354.         beq.b    .nofast
  1355.     
  1356.         move.l    OSM_EXPSIZE,d0
  1357.         SUBQ.L    #8,D0
  1358.         LSR.L    #3,D0            ;OK, AMOUNT OF BITS TO CLEAR IS IN D0
  1359.         LSR.L    #3,D0            ;AMOUNT OF BYTES TO CLEAR IN D0
  1360. .2        CLR.B    (A0)+
  1361.         SUBQ.L    #1,D0
  1362.         BPL.S    .2
  1363.         MOVE.L    A0,ALLOCFASTMTABSIZE    ; not real size!
  1364.  
  1365.         move.l    OSM_EXPSIZE,d0
  1366.         add.l    OSM_EXPMEM,D0        ; adds expbase
  1367.         SUB.L    A0,D0            ; substracts expbase+tabsize
  1368.         SUB.L    #$100,D0        ;$100 safety
  1369.  
  1370.         MOVE.L    A0,A1            ;FREE APPROPRIATE SIZE
  1371.         BSR    _FreeMem
  1372.  
  1373. .nofast:
  1374.     ;allocate stack
  1375.         move.l    #USPLENGTH,d0
  1376.         moveq    #MEMF_PUBLIC,d1    ; MEMF_CHIP removed
  1377.         bsr    _AllocMem
  1378.         move.l    d0,a0
  1379.         MOVE.L    A0,_EXECLIBTASK+TC_SPLOWER
  1380.         add.l    #USPLENGTH-4,a0
  1381.         move.l    a0,usp
  1382.         MOVE.L    A0,_EXECLIBTASK+pr_StackBase
  1383.         MOVE.L    #USPLENGTH-$20,(A0)+
  1384.         MOVE.L    A0,_EXECLIBTASK+TC_SPUPPER
  1385.  
  1386.     ;get whdload vars
  1387.         lea    _tags,a0
  1388.         move.l    _RESLOAD,a1
  1389.         jsr    (resload_Control,a1)
  1390.  
  1391.     ;init libraries
  1392.         bsr    CIAAINIT        ;required because int handling
  1393.         bsr    CIABINIT        ;required because int handling
  1394.         bsr    EXECINIT
  1395.         bsr    GFXINIT            ;required for display init (copper)
  1396.         IFNE    CORRECTDEVICES
  1397.         BSR.W    MAKEDEVICELIST
  1398.         ENDC
  1399.  
  1400.     ; acknowledge keyboard
  1401.  
  1402.         bsr    AckKeyboard
  1403.     
  1404.     ; enables interrupts (first clear all requests)
  1405.         move.w    #$7FFF,(_custom+intreq)
  1406.         move.w    #INTF_SETCLR!INTF_INTEN!INTF_EXTER!INTF_VERTB!INTF_PORTS,(_custom+intena)
  1407.  
  1408.  
  1409.     ;flush caches
  1410.         move.l    (_RESLOAD),a0
  1411.         jsr    (resload_FlushCache,a0)
  1412.  
  1413.     ;prepare return values
  1414.     ; > A1: diskio of trackdisk.device, filled with READ command
  1415.     ; > A6: execbase
  1416.  
  1417.         move.l    $4.W,a6
  1418.         move.l    #$400+IOTD_SIZE,d0
  1419.         moveq    #0,d1
  1420.         bsr    _AllocMem
  1421.         move.l    d0,a2
  1422.         lea    (_tdname),a0
  1423.         moveq    #0,d0
  1424.         move.l    a2,a1
  1425.         moveq    #0,d1
  1426.         JSRLIB    OpenDevice
  1427.         move.l    a2,a1
  1428.         lea    (IOTD_SIZE,a2),a0
  1429.         move.l    a0,(IO_DATA,a1)
  1430.         move.l    #$400,(IO_LENGTH,a1)
  1431.         clr.l    (IO_OFFSET,a1)
  1432.         move.w    #CMD_READ,(IO_COMMAND,a1)
  1433.  
  1434.         movem.l    (a7)+,d0-a0/a2-a5
  1435.  
  1436.     ; ready for multitasking
  1437.  
  1438.         move.w    #1,_Multitasks
  1439.  
  1440.         rts
  1441.  
  1442. ; Added by JOTD: enter debugger (useful for debugging with JST)
  1443.  
  1444. EnterDebugger:
  1445.     tst.l    OSM_DEBUGENTRY
  1446.     beq.b    .exit
  1447.     pea    .exit(pc)
  1448.     move.l    OSM_DEBUGENTRY,-(A7)
  1449.     rts
  1450. .exit
  1451.     rts
  1452.  
  1453. **************************************************************************
  1454. *   MISC FUNCTIONS                                                       *
  1455. **************************************************************************
  1456. ;-----------------------------------------------
  1457. ; IN:    D0 = ULONG size of jmp table
  1458. ;    D1 = ULONG size of variable area
  1459. ;    A0 = CPTR  subsystem name
  1460. ; OUT:    D0 = APTR  librarybase
  1461.  
  1462. _InitStruct    movem.l    d0-d1/a0,-(a7)
  1463.         add.l    d1,d0
  1464.         move.l    #MEMF_CLEAR,d1    ;changed by JOTD
  1465.         bsr    _AllocMem
  1466.         move.l    d0,a0            ;jmp table start
  1467.                 move.l    d0,a1
  1468.         add.l    (a7),a1            ;jmp table end
  1469.  
  1470.         lea    _LVOFail,a2
  1471. .1        move.w    #$4EB9,(A0)+
  1472.         move.l    a2,(a0)+
  1473.         cmp.l    a0,a1
  1474.         bhi    .1
  1475.         move.l    (8,a7),-4(a0)        ;name of library
  1476.         move.l    a0,(A7)            ;library base
  1477.         move.l    a0,a1            ;variables start
  1478.         add.l    (4,a7),a1        ;variables end
  1479. .2        move.w    #$eeee,(a0)+
  1480.         cmp.l    a0,a1
  1481.         bhi    .2
  1482.         MOVEM.L    (A7)+,D0/D1/A0
  1483.         rts
  1484.  
  1485. _InitLibrary    BSR.S    _InitStruct
  1486.         MOVE.L    D0,A1
  1487.         MOVE.B    #NT_LIBRARY,LN_TYPE(A1)
  1488.         SF    LN_PRI(A1)
  1489.         move.l    A0,LN_NAME(A1)        ;name of library
  1490.         LEA.L    _LIBLIST(PC),A0
  1491.         BSR.W    _ADDTAIL
  1492.         rts
  1493.  
  1494.     IFNE    CORRECTDEVICES
  1495. _InitDevice    BSR.S    _InitStruct
  1496.         MOVE.L    D0,A1
  1497.         MOVE.B    #NT_LIBRARY,LN_TYPE(A1)
  1498.         SF    LN_PRI(A1)
  1499.         move.l    A0,LN_NAME(A1)        ;name of library
  1500.         LEA.L    _DEVLISTC(PC),A0
  1501.         BSR.W    _ADDTAIL
  1502.         rts
  1503.     ENDC
  1504.  
  1505. _LVOFail    exg.l    d0,a6
  1506.         sub.l    d0,(a7)            ;LVO
  1507.         exg.l    d0,a6
  1508.         subq.l    #6,(a7)
  1509.         move.l    (-4,a6),-(a7)        ;name of library
  1510. _emufail    pea    TDREASON_OSEMUFAIL
  1511.         move.l    _RESLOAD(pc),-(a7)
  1512.         addq.l    #resload_Abort,(a7)
  1513. _rts        rts
  1514.  
  1515. EMUFAIL        MACRO
  1516.         pea    \1
  1517.         pea    \2
  1518.         bra    _emufail
  1519.         ENDM
  1520. EXECFAIL    MACRO
  1521.         EMUFAIL    \1,_execname
  1522.         ENDM
  1523. GFXFAIL        MACRO
  1524.         EMUFAIL    \1,_gfxname
  1525.         ENDM
  1526.  
  1527. ;-----------------------------------------------
  1528. ; IN:    A0 = CPTR  string 1
  1529. ;    A0 = CPTR  string 2
  1530. ; OUT:    D0 = LONG  0 if strings are equal
  1531.  
  1532. _strcmp        movem.l    a0-a1,-(a7)
  1533. .1        cmpm.b    (a0)+,(a1)+
  1534.         bne    .not
  1535.         tst.b    (-1,a0)
  1536.         bne.b    .1
  1537.         moveq.l    #0,d0        ; bugfix: move.w #0,d0 was there!
  1538.         movem.l    (a7)+,a0-a1
  1539.         rts
  1540.  
  1541. .not        moveq.l    #-1,d0
  1542.         movem.l    (a7)+,a0-a1
  1543.         rts
  1544.  
  1545. ;-----------------------------------------------
  1546. ; wait for vertical blank
  1547.  
  1548. _waitvb
  1549. .1        btst    #0,(_custom+vposr+1)
  1550.         beq    .1
  1551. .2        btst    #0,(_custom+vposr+1)
  1552.         bne    .2
  1553.         rts
  1554.  
  1555. _ill        illegal
  1556.  
  1557. **************************************************************************
  1558. *   EXEC LIBRARY                                                         *
  1559. **************************************************************************
  1560.  
  1561. EXEC2INIT
  1562.     move.l    A6,D0    ; NightShift tries to open exec.library!
  1563.     rts
  1564.  
  1565. **************************************************************************
  1566. *   INITIALIZATION                                                       *
  1567. **************************************************************************
  1568.  
  1569. EXECINIT
  1570.     ;    move.l    4,d0
  1571.     ;    btst    #0,d0
  1572.     ;    bne    .init
  1573.     ;    rts
  1574.  
  1575. .init        lea    _EXECMINSIZE(PC),A0
  1576.         LEA.L    _EXECLIBBASE(PC),A1
  1577.         lea    _LVOFail,a2
  1578. .1        move.w    #$4EB9,(A0)+
  1579.         move.l    a2,(a0)+
  1580.         cmp.l    a0,a1
  1581.         bhi    .1
  1582.         move.l    #_execname,-4(a0)    ;name of library
  1583.         MOVE.L    a1,a0
  1584.         move.l    a0,$4.W
  1585.         
  1586.         patch    _LVORawDoFmt(A0),RAWDOFMT
  1587.         patch    _LVOOldOpenLibrary(a0),OPENLIB
  1588.         patch    _LVOOpenLibrary(a0),OPENLIB
  1589.         patch    _LVOCloseLibrary(A0),MYRTS
  1590.         patch    _LVODebug(A0),_Debug
  1591.  
  1592.         patch    _LVOAllocMem(a0),_AllocMem
  1593.         patch    _LVOFreeMem(a0),_FreeMem
  1594.         patch    _LVOAllocVec(a0),_AllocVec
  1595.         patch    _LVOFreeVec(a0),_FreeVec
  1596.         patch    _LVOAvailMem(a0),_AvailMem
  1597.         patch    _LVOTypeOfMem(a0),TYPEOFMEM
  1598.         patch    _LVOAllocAbs(a0),_AllocAbs
  1599.         patch    _LVOCopyMem(A0),_COPYMEM
  1600.         patch    _LVOCopyMemQuick(A0),_COPYMEMQUICK
  1601.         patch    _LVOAllocEntry(a0),ALLOCENTRY
  1602.         patch    _LVOFreeEntry(a0),FREEENTRY
  1603.  
  1604.         patch    _LVOForbid(a0),_Forbid
  1605.         patch    _LVOPermit(a0),_Permit
  1606.         patch    _LVOCause(A0),_Cause
  1607.         patch    _LVOSetIntVector(A0),_SETINTVECTOR
  1608.         patch    _LVOAddIntServer(a0),_AddIntServer
  1609.         patch    _LVORemIntServer(a0),_RemIntServer
  1610.         patch    -$24(A0),_ExitIntr
  1611.         patch    _LVOEnqueue(a0),_Enqueue
  1612.         patch    _LVOOpenResource(a0),OPENRES
  1613.         patch    _LVOSupervisor(a0),_Supervisor
  1614.         patch    _LVOSuperState(a0),_SuperState
  1615.         patch    _LVOUserState(a0),_UserState
  1616.         patch    _LVOFindTask(a0),_FindTask
  1617.         patch    _LVORemTask(a0),MYRTS
  1618.         patch    _LVOSetTaskPri(A0),MYRTZ
  1619.  
  1620.         patch    _LVOAddPort(a0),_AddPort
  1621.         patch    _LVOOpenDevice(a0),_OpenDevice
  1622.         patch    _LVODoIO(a0),_DoIO
  1623.         patch    _LVOWaitIO(A0),_WAITIO    ; uncommented by JOTD
  1624.         patch    _LVOAbortIO(A0),_ABORTIO
  1625.         patch    _LVOCheckIO(A0),_CHECKIO    ; added by JOTD
  1626.         patch    _LVOSendIO(a0),_SendIO
  1627.         patch    _LVOCloseDevice(a0),MYRTS
  1628.         patch    _LVORemPort(a0),_RemPort
  1629.  
  1630.         patch    _LVOAllocSignal(a0),_AllocSignal
  1631.         patch    _LVOFreeSignal(a0),_FreeSignal
  1632.         patch    _LVOSetSignal(a0),_SetSignal
  1633.         patch    _LVODisable(a0),_Disable
  1634.         patch    _LVOEnable(a0),_Enable
  1635. ;;;        patch    _LVOWaitPort(A0),_WaitPort    ; unplugged ATM
  1636.         patch    _LVOGetMsg(A0),_GetMsg
  1637.         patch    _LVOPutMsg(A0),_PutMsg
  1638.         patch    _LVOReplyMsg(A0),_ReplyMsg
  1639.         patch    _LVOCreateIORequest(A0),_CreateIORequest
  1640.         patch    _LVODeleteIORequest(A0),_DeleteIORequest
  1641.         patch    _LVOCreateMsgPort(A0),_CreateMsgPort
  1642.         patch    _LVODeleteMsgPort(A0),_DeleteMsgPort
  1643. ;;;        patch    _LVOAddTask(a0),_AddTask    ; added by JOTD, unplugged
  1644.  
  1645.         patch    _LVOInsert(A0),_INSERT
  1646.         patch    _LVOAddHead(A0),_ADDHEAD
  1647.         patch    _LVOAddTail(A0),_ADDTAIL
  1648.         patch    _LVORemove(A0),_REMOVE
  1649.         patch    _LVORemHead(A0),_REMHEAD
  1650.         patch    _LVORemTail(A0),_REMTAIL
  1651.         patch    _LVOFindName(A0),_FINDNAME
  1652.         patch    _LVOFindPort(A0),_FINDPORT
  1653.  
  1654.         patch    _LVOCacheControl(A0),_CacheControl    ; added by JOTD
  1655.         patch    _LVOCacheClearU(A0),_CacheClearU    ; added by JOTD
  1656.         patch    _LVOSetFunction(A0),_SetFunction    ; added by JOTD
  1657.         patch    _LVOFindResident(A0),_FindResident    ; added by JOTD
  1658.         patch    _LVOStackSwap(a0),STACKSWAP        ; added by JOTD (PGAGolf)
  1659.         patch    _LVOGetCC(a0),_GETCC            ; added by JOTD (Lunar-C)
  1660.  
  1661. ;;;;;        patch    _LVOWait(A0),MYRTS    ; JOTD, dummy!
  1662.  
  1663.         move.w    (_attnflags),(AttnFlags,a0)
  1664.  
  1665.         MOVE.L    _Slave(PC),A1
  1666.         MOVE.L    ws_BaseMemSize(A1),A1
  1667.         MOVE.L    A1,SysStkUpper(A0)
  1668.         MOVE.L    A1,MaxLocMem(A0)
  1669.         LEA.L    -$1000(A1),A1
  1670.         MOVE.L    A1,SysStkLower(A0)
  1671.         MOVE.L    _monitor(PC),D0
  1672.         MOVEQ.L    #60,D1
  1673.         AND.L    #$FFFF1000,D0
  1674.         CMP.L    #$21000,D0
  1675.         BNE.S    .nopal
  1676.         moveq.l    #50,D1
  1677. .nopal
  1678.         move.b    d1,VBlankFrequency(A0)
  1679.  
  1680.         ; JOTD, for timer.device
  1681.  
  1682.         move.l    #1000000,D0
  1683.         divu    D1,D0        ; number of microsecs between VBL interrupt
  1684.         move.w    D0,microsec_vbl_interval+2
  1685.  
  1686.     ;clear int table
  1687.         lea    (IntVects,a0),a1
  1688.         moveq    #16*IV_SIZE/4-1,d0
  1689. .c1        MOVE.L    #VAL_DDDDDDDD,(a1)+
  1690.         dbf    d0,.c1
  1691.  
  1692.     ;init int table
  1693.         LEA.L    (IVPORTS,A0),A1
  1694.         MOVE.L    A1,(IV_DATA,a1)
  1695.         MOVE.L    #INT_SERVER,(IV_CODE,A1)
  1696.         move.l    #INTSERVNODE_CIAA,(IV_NODE,a1)
  1697.  
  1698.         LEA.L    (IVCOPER,A0),A1
  1699.         MOVE.L    A1,(IV_DATA,a1)
  1700.         MOVE.L    #INT_SERVER,(IV_CODE,A1)
  1701.         CLR.L    (IV_NODE,a1)
  1702.  
  1703.         LEA.L    (IVVERTB,A0),A1
  1704.         MOVE.L    A1,(IV_DATA,a1)
  1705.         MOVE.L    #INT_SERVER,(IV_CODE,A1)
  1706.         CLR.L    (IV_NODE,a1)
  1707.  
  1708.         LEA.L    (IVEXTER,A0),A1
  1709.         MOVE.L    A1,(IV_DATA,a1)
  1710.         MOVE.L    #INT_SERVER,(IV_CODE,A1)
  1711.         move.l    #INTSERVNODE_CIAB,(IV_NODE,a1)
  1712.  
  1713.         LEA.L    (IVAUD0,A0),A1
  1714.         MOVE.L    A1,(IV_DATA,a1)
  1715.         MOVE.L    #INT_SERVER,(IV_CODE,A1)
  1716.         move.l    #INTSERVNODE_AUDIO0,(IV_NODE,a1)
  1717.  
  1718.         LEA.L    (IVAUD1,A0),A1
  1719.         MOVE.L    A1,(IV_DATA,a1)
  1720.         MOVE.L    #INT_SERVER,(IV_CODE,A1)
  1721.         move.l    #INTSERVNODE_AUDIO1,(IV_NODE,a1)
  1722.  
  1723.         LEA.L    (IVAUD2,A0),A1
  1724.         MOVE.L    A1,(IV_DATA,a1)
  1725.         MOVE.L    #INT_SERVER,(IV_CODE,A1)
  1726.         move.l    #INTSERVNODE_AUDIO2,(IV_NODE,a1)
  1727.  
  1728.         LEA.L    (IVAUD3,A0),A1
  1729.         MOVE.L    A1,(IV_DATA,a1)
  1730.         MOVE.L    #INT_SERVER,(IV_CODE,A1)
  1731.         move.l    #INTSERVNODE_AUDIO3,(IV_NODE,a1)
  1732.  
  1733.     ;init hardware ints
  1734.         move.l    #INT_64,$64.W
  1735.         move.l    #INT_68,$68.W
  1736.         move.l    #INT_6c,$6C.W
  1737.         MOVE.L    #INT_70,$70.W
  1738.         move.l    #INT_78,$78.W
  1739.  
  1740.         move.b    #$1f,$bfed01
  1741.  
  1742.         MOVE.B    #$80+CIAA_INT_VAL,$BFED01
  1743.  
  1744.         tst.B    $BFED01
  1745.         move.b    #$1f,$bfdd00
  1746. ;        MOVE.B    #$87,$BFDD00    ; OS doesn't enable TOD int (Mr.Larmer)
  1747.         MOVE.B    #$8+CIAB_INT_VAL,$BFDD00
  1748.         tst.b    $bfdd00
  1749.         
  1750.         move.w    #$7fff,(_custom+intena)
  1751.  
  1752.         rts
  1753.  
  1754.     IFNE    CORRECTDEVICES
  1755. MAKEDEVICELIST
  1756.         MOVE.L    4.W,A0
  1757.         move.l    #$24,d0
  1758. ;        move.l    #LIB_SIZE,d1
  1759.         move.l    #LIB_SIZE+$a,d1
  1760.         lea    _tdname(PC),a0
  1761.         bsr    _InitDevice
  1762.         MOVE.L    d0,_tddevtable+4
  1763.         MOVE.L    D0,A0
  1764.         CLR.L    $28(A0)            ;DUE LORDS OF WAR
  1765.         MOVE.L    #_TDDMSGPORT,$24(A0)    ;-"-
  1766.  
  1767.         ; keyboard device
  1768.  
  1769.         move.l    #$24,d0
  1770. ;        move.l    #LIB_SIZE,d1
  1771.         move.l    #$14C,d1
  1772.         lea    _kbdevname(PC),a0
  1773.         bsr    _InitDevice
  1774.         MOVE.L    d0,_kbdevtable+4
  1775.  
  1776.         move.l    d0,INTSERVNODE_SP+IS_DATA
  1777.  
  1778.         move.l    d0,a0
  1779.         lea    $70(a0),a0
  1780.         move.l    #$14C-$70-1,d0
  1781. .clear
  1782.         clr.b    (a0)+
  1783.         dbf    d0,.clear
  1784.  
  1785.         ; input.device
  1786.  
  1787.         move.l    #$24,d0
  1788.         move.l    #LIB_SIZE,d1
  1789.         lea    _inpname(PC),a0
  1790.         bsr    _InitDevice
  1791.         MOVE.L    d0,_inpdevtable+4
  1792.  
  1793.         ; audio.device
  1794.  
  1795.         move.l    #$24,d0
  1796.         move.l    #LIB_SIZE,d1
  1797.         lea    _audname(PC),a0
  1798.         bsr    _InitDevice
  1799.         MOVE.L    d0,_auddevtable+4
  1800.  
  1801.         MOVE.L    D0,A0
  1802.         patch    -$1E(A0),_SENDAUDIO
  1803.  
  1804.         ; cd.device, added by JOTD
  1805.  
  1806.         move.l    #$24,d0
  1807.         move.l    #LIB_SIZE,d1
  1808.         lea    _cdname(PC),a0
  1809.         bsr    _InitDevice
  1810.         MOVE.L    d0,_cddevtable+4
  1811.  
  1812.         ; cdtv.device, added by JOTD
  1813.  
  1814.         move.l    #$24,d0
  1815.         move.l    #LIB_SIZE,d1
  1816.         lea    _cdtvname(PC),a0
  1817.         bsr    _InitDevice
  1818.         MOVE.L    d0,_cdtvdevtable+4
  1819.  
  1820.         ; dummy console.device
  1821.  
  1822.         move.l    #$24,d0
  1823.         move.l    #LIB_SIZE,d1
  1824.         lea    _condevname(PC),a0
  1825.         bsr    _InitDevice
  1826.         MOVE.L    d0,_condevtable+4
  1827.  
  1828.         move.l    D0,A0
  1829.         patch    -$24(a0),_RawKeyConvert     ; needed by Wings. RawKeyConvert, but wrong offset!
  1830.         
  1831.         ; timer.device
  1832.  
  1833.         move.l    #$24,d0
  1834. ;;        move.l    #LIB_SIZE,d1
  1835.         move.l    #$C4,d1            ; Jeff, to store ciaa.resource pointer
  1836.         lea    _timerdevname(PC),a0
  1837.         bsr    _InitDevice
  1838.         MOVE.L    d0,_timerdevtable+4
  1839.  
  1840.         MOVE.L    D0,A0
  1841.         patch    _LVOReadEClock(A0),_READECLOCK    ; ROM 3.1 at $FC10F8
  1842.  
  1843.         ; stores execbase
  1844.  
  1845.         move.l    $4.W,$24(A0)
  1846.  
  1847.         ; opens ciaa.resource
  1848.  
  1849.         bsr    .openciaa
  1850.  
  1851.         ; enable CIA-B timer B: starts running
  1852.  
  1853.         move.b    #%00000001,$bfef01
  1854.  
  1855.         RTS
  1856.  
  1857.     ENDC
  1858.  
  1859. .openciaa:
  1860.     MOVEM.L    A0/A1/D1,-(A7)
  1861.     LEA    _ciaaname,A1
  1862.     bsr    OPENRES
  1863.     MOVEM.L    (A7)+,A0/A1/D1
  1864.     move.l    D0,$C0(A0)
  1865.     rts
  1866.     
  1867. MYRTZ        MOVEQ.L    #0,D0
  1868. MYRTS        RTS
  1869.  
  1870. **************************************************************************
  1871. *   MISC EXEC FUCTIONS                                                   *
  1872. **************************************************************************
  1873.  
  1874. ; < A0: new stack structure
  1875. ; (ripped from OS3.1 ROM)
  1876.  
  1877. STACKSWAP:
  1878.     MOVE.L    $0114(A6),A1
  1879.     
  1880.     DISABLE
  1881.  
  1882.     MOVE.L    $003E(A1),D0
  1883.     MOVE.L    $0004(A0),$003E(A1)
  1884.     MOVE.L    D0,$0004(A0)
  1885.     MOVE.L    $003A(A1),D0
  1886.     MOVE.L    (A0),$003A(A1)
  1887.     MOVE.L    D0,(A0)
  1888.     MOVE.L    $0008(A0),$0036(A1)
  1889.     MOVE.L    (A0),A1
  1890.     MOVE.L    #$BAD1BAD3,(A1)    ; illegal value
  1891.     MOVE.L    $0008(A0),A1
  1892.     MOVE.L    (A7)+,D0
  1893.     MOVE.L    A7,$0008(A0)
  1894.     MOVE.L    D0,-(A1)
  1895.     MOVE.L    A1,A7
  1896.  
  1897.     ENABLE
  1898.     RTS    
  1899.  
  1900.  
  1901. _CacheControl:
  1902. _CacheClearU:
  1903.         movem.l    A0,-(A7)
  1904.         move.l    (_RESLOAD),a0
  1905.         jsr    (resload_FlushCache,a0)
  1906.         movem.l    (A7)+,A0
  1907.         rts
  1908.  
  1909. _FindResident:
  1910.     moveq.l    #0,D0
  1911.     rts
  1912.  
  1913. _SetFunction                ; added by JOTD (Banshee)
  1914.         movem.l    D1,-(A7)
  1915.         move.l    2(A1,A0.W),D1
  1916.         move.l    D0,2(A1,A0.W)    ; changed
  1917.         move.l    D1,D0        ; old function
  1918.         movem.l    (A7)+,D1
  1919.         rts
  1920.  
  1921. _Supervisor    move.l    $bc,.s1
  1922.         move.l    #.c1,$bc.w
  1923.         trap    #15
  1924.         rts
  1925. .c1        move.l    .s1,$bc.w
  1926.         jmp    (a5)
  1927. .s1        dc.l    0
  1928.  
  1929. _SuperState    MOVE.L    A5,A0
  1930.         LEA       .ac,A5
  1931.         JMP       _LVOSupervisor(A6)
  1932. .ac        MOVE.L    A0,A5
  1933.         MOVEQ     #$00,D0
  1934.         BSET      #$05,(A7)
  1935.         BNE.S     .ca
  1936.         MOVE.W    (A7)+,SR
  1937.         MOVE.L    A7,D0
  1938.         MOVE.L    USP,A7
  1939.         BTST      #$00,$0129(A6)
  1940.         BEQ.S     .c6
  1941.         ADDQ.L    #$2,D0
  1942. .c6        ADDQ.L    #$4,D0
  1943.         RTS
  1944. .ca        RTE
  1945.  
  1946. _UserState    MOVE.L    (A7)+,A0
  1947.         MOVE.L    A7,USP
  1948.         MOVE.L    D0,A7
  1949.         ANDI.W    #$dfff,SR
  1950.         JMP       (A0)
  1951.  
  1952. _CreateIORequest
  1953.     movem.l    D2/D3,-(SP)
  1954.     move.l    D0,D2
  1955.     move.l    A0,D3
  1956.     beq.s    .error
  1957.     move.l    #$10001,D1
  1958.     jsr    _LVOAllocMem(A6)
  1959.     movea.l    D0,A0
  1960.     tst.l    D0
  1961.     beq.s    .error
  1962.     move.b    #NT_REPLYMSG,LN_TYPE(A0)
  1963.     move.l    D3,MN_REPLYPORT(A0)
  1964.     move.w    D2,MN_LENGTH(A0)
  1965. .error
  1966.     move.l    A0,D0
  1967.     movem.l    (SP)+,D2/D3
  1968.     rts
  1969.  
  1970. _DeleteIORequest
  1971.     move.l    A0,D0
  1972.     beq.s    .abort
  1973.     moveq    #-1,D0
  1974.     move.l    D0,LN_SUCC(A0)
  1975.     move.l    D0,IO_DEVICE(A0)
  1976.     moveq    #0,D0
  1977.     move.w    MN_LENGTH(A0),D0
  1978.     movea.l    A0,A1
  1979.     jsr    _LVOFreeMem(A6)
  1980. .abort
  1981.     rts
  1982.  
  1983. _CreateMsgPort
  1984.     moveq    #MP_SIZE,D0    ;$22,D0
  1985.     move.l    #$10001,D1
  1986.     jsr    _LVOAllocMem(A6)
  1987.     move.l    D0,-(SP)
  1988.     beq.s    .error
  1989.     moveq    #-1,D0
  1990.     jsr    _LVOAllocSignal(A6)
  1991.     movea.l    (SP),A0
  1992.     move.b    #NT_MSGPORT,LN_TYPE(A0)
  1993.     move.b    #PA_SIGNAL,MP_FLAGS(A0)
  1994.     move.b    D0,MP_SIGBIT(A0)
  1995.     bmi.s    .abort
  1996.     move.l    ThisTask(A6),MP_SIGTASK(A0)
  1997.     lea    MP_MSGLIST(A0),A1
  1998.     INITLIST    A1
  1999. .error
  2000.     move.l    (SP)+,D0
  2001.     rts
  2002.  
  2003. .abort
  2004.     moveq    #MP_SIZE,D0
  2005.     movea.l    A0,A1
  2006.     jsr    _LVOFreeMem(A6)
  2007.     clr.l    (SP)
  2008.     bra.s    .error
  2009.  
  2010. _DeleteMsgPort
  2011.     move.l    A0,-(SP)
  2012.     beq.s    .abort
  2013.     moveq    #0,D0
  2014.     move.b    MP_SIGBIT(A0),D0
  2015.     jsr    _LVOFreeSignal(A6)
  2016.     movea.l    (SP),A1
  2017.     moveq    #-1,D0
  2018.     move.l    D0,MP_MSGLIST(A1)
  2019.     move.l    D0,LN_SUCC(A1)
  2020.     moveq    #MP_SIZE,D0
  2021.     jsr    _LVOFreeMem(A6)
  2022. .abort
  2023.     addq.l    #4,SP
  2024.     rts
  2025.  
  2026.  
  2027. **************************************************************************
  2028. *   Task related functions                                               *
  2029. **************************************************************************
  2030.  
  2031. _AllocSignal
  2032.     movea.l    ThisTask(A6),A1
  2033.     move.l    TC_SIGALLOC(A1),D1
  2034.     cmp.b    #-1,D0
  2035.     beq.s    .search
  2036.     bset    D0,D1
  2037.     beq.s    .free
  2038.     bra.s    .err
  2039.  
  2040. .search
  2041.     moveq.l    #$20-1,D0
  2042. .search2
  2043.     bset    D0,D1
  2044.     beq.s    .free
  2045.     dbra    D0,.search2
  2046. .err
  2047.     moveq.l    #-1,D0
  2048.     rts
  2049.  
  2050. .free
  2051.     move.l    D1,TC_SIGALLOC(A1)
  2052.     moveq    #-1,D1
  2053.     bclr    D0,D1
  2054.     and.l    D1,TC_SIGRECVD(A1)
  2055.     and.l    D1,TC_SIGEXCEPT(A1)
  2056.     and.l    D1,TC_SIGWAIT(A1)
  2057.     rts
  2058.  
  2059. _FreeSignal
  2060.         cmp.b    #$FF,D0
  2061.         beq.s    .skip            ; Mr Larmer: no more error
  2062.         movea.l    ThisTask(A6),A1
  2063.         move.l    TC_SIGALLOC(A1),D1
  2064.         bclr    D0,D1
  2065.         move.l    D1,TC_SIGALLOC(A1)
  2066. .skip
  2067.         rts
  2068.  
  2069.  
  2070. _SetSignal    MOVE.L    ThisTask(A6),A0
  2071.         MOVE.L    TC_SIGRECVD(A0),-(A7)
  2072.         AND.L    D1,D0
  2073.         NOT.L    D1
  2074.         AND.L    TC_SIGRECVD(A0),D1
  2075.         OR.L    D0,D1
  2076.         MOVE.L    D1,TC_SIGRECVD(A0)
  2077.         MOVE.L    (A7)+,d0
  2078.         rts
  2079.  
  2080. _Disable
  2081.     DISABLE        ; we use the macro!
  2082.     rts
  2083. _Enable
  2084.     ENABLE
  2085.     rts
  2086.  
  2087. _Forbid:
  2088.     ADDQ.B  #1,TDNestCnt(A6)
  2089.     rts
  2090.  
  2091. _Permit:
  2092.     SUBQ.B  #1,TDNestCnt(A6)
  2093.     BPL.B    .skip
  2094.  
  2095.     ; checks if interrupts are allowed
  2096.  
  2097.     tst.b    IDNestCnt(A6)
  2098.     bpl.b    .skip
  2099.  
  2100.     ; checks if switch flag is pending (and resets it)
  2101.  
  2102.     bclr    #7,AttnResched(A6)
  2103.     beq.b    .skip
  2104.  
  2105.     ; must switch
  2106.  
  2107.     bsr    _Switch
  2108. .skip
  2109.     rts
  2110.  
  2111. ; Switch, private, AROS implementation
  2112.  
  2113. _Switch:
  2114.     move.l    ThisTask(a6),A1
  2115.     cmp.b    #TS_RUN,(TC_STATE,A1)
  2116.     bne.b    .fail            ; impossible since task would not be there
  2117.  
  2118.     ; freeze the task state
  2119.  
  2120.     move.b    #TS_READY,(TC_STATE,A1)
  2121.  
  2122.     ; Use Reschedule() to put the task in the correct list
  2123.  
  2124.     bsr    _Reschedule
  2125.  
  2126.     ; sets new, active Task in exec structure
  2127.  
  2128.     bsr    _Dispatch
  2129. .skip
  2130.     rts
  2131.  
  2132. .fail:
  2133.     EMUFAIL    _LVOSwitch,_execname
  2134.  
  2135. ; Dispatch, private, AROS implementation
  2136.  
  2137. _Dispatch:
  2138.     move.l    (ThisTask,A6),A1    ; current, old task
  2139.     lea    (TaskReady,A6),A0    ; taskready list
  2140.     JSRLIB    RemHead            ; next ready task
  2141.     tst.l    D0            ; is there any more task
  2142.     beq.b    .fail            ; no more task to dispatch
  2143.  
  2144.     ; copy the sysbase nest counts in the old, about to be switched
  2145.     ; task nest counts
  2146.  
  2147.     move.b    (TDNestCnt,A6),(TC_TDNESTCNT,A1)
  2148.     move.b    (IDNestCnt,A6),(TC_IDNESTCNT,A1)
  2149.  
  2150.     move.l    D0,A0    ; incoming task
  2151.  
  2152.     move.b    #TS_RUN,(TC_STATE,A0)
  2153.  
  2154.     ; copy the task's nest counts to exec nest counts
  2155.  
  2156.     move.b    (TC_TDNESTCNT,A0),(TDNestCnt,A6)
  2157.     move.b    (TC_IDNESTCNT,A0),(IDNestCnt,A6)
  2158.  
  2159.     ; now this is the current task
  2160.  
  2161.     move.l    A0,(ThisTask,A6)
  2162.  
  2163.     ; we should perform stack checks and increase some counters, but
  2164.     ; we don't care
  2165.  
  2166.  
  2167.     ; ok, now we get task's user SP
  2168.  
  2169.  
  2170.     rts
  2171.  
  2172. .fail:
  2173.     EMUFAIL    _LVODispatch,_execname
  2174.  
  2175. ; Reschedule, private
  2176. ; < A1: task
  2177.  
  2178. _Reschedule:
  2179.     move.l    ThisTask(a6),A1
  2180.     move.b    (TC_STATE,A1),D0
  2181.     cmp.b    #TS_READY,D0
  2182.     bne.b    .s1
  2183.     ; insert task in task ready list
  2184.  
  2185.     lea    (TaskReady,A6),A0
  2186.     JSRLIB    Enqueue
  2187.     bra.b    .exit
  2188. .s1
  2189.     cmp.b    #TS_WAIT,D0
  2190.     bne.b    .s2
  2191.     ; insert task in task wait list
  2192.  
  2193.     lea    (TaskWait,A6),A0
  2194.     JSRLIB    Enqueue
  2195. .s2
  2196. .exit
  2197.     rts
  2198.  
  2199. ; handle task switching during interrupts
  2200. ; trashes D0-D1/A0-A1
  2201.  
  2202. HandleTaskSwitching:
  2203.     ; unimplemented !!!
  2204.     rts
  2205.  
  2206.     move.l    $4.W,A6
  2207.  
  2208.     ; sets pending switch flag
  2209.  
  2210.     bset    #7,AttnResched(A6)
  2211.  
  2212.     ; check if Forbid count is >=0
  2213.  
  2214.     tst.b    TDNestCnt(A6)
  2215.     bpl.b    .noswitch    ; nested Forbid(), don't switch
  2216.  
  2217.     ; interrupted: tries to switch tasks if any pending
  2218.  
  2219.     lea    4(A7),A0
  2220.  
  2221.     move.w    (60,A0),D0    ; get SR when interrupt occurred
  2222.     btst    #13,D0        ; was supervisor?
  2223.     bne.b    .noswitch    ; if so, don't switch right now
  2224.  
  2225.     ; user mode, not forbidden
  2226.     
  2227.     move.l    (62,A0),D0    ; user PC where the interrupt took place
  2228.  
  2229.     ; swap current task
  2230.  
  2231.     bclr    #7,AttnResched(A6)
  2232.  
  2233. .noswitch
  2234.     rts
  2235.  
  2236. ; AddTask, added by JOTD
  2237. ; with great help of AROS implementation
  2238.  
  2239. ; task regsave struct offsets
  2240.  
  2241.     STRUCTURE    TASKSAVE,0
  2242.     ULONG    TSV_CURRENTPC
  2243.     ULONG    TSV_ENDPC
  2244.     UWORD    TSV_SR        ; always user SR
  2245.     STRUCT    TSV_DR,4*8
  2246.     STRUCT    TSV_AR,4*7
  2247.     LABEL    TSV_SIZE
  2248.  
  2249. ; < A1: task structure
  2250. ; < A2: initial PC
  2251. ; < A3: final PC (finalizer) or 0 if no finalizer
  2252. ; > D0: pointer on task or NULL if failed
  2253.  
  2254.     ifeq 1
  2255. _AddTask:
  2256.     movem.l    D2/A4,-(A7)
  2257.     move.l    A1,A4
  2258.  
  2259.     bsr    EnterDebugger
  2260.  
  2261.     move.b    #NT_TASK,(LN_TYPE,A4)
  2262.     tst.l    (LN_NAME,A4)
  2263.     bne.b    .named
  2264.     move.l    #.defaultname,(LN_NAME,A4)    ; provides default name to the task
  2265. .named
  2266.     tst.l    (TC_SIGALLOC,A4)
  2267.     bne.b    .sigset
  2268.     move.l    #$FFFF,(TC_SIGALLOC,A4)
  2269. .sigset
  2270.     ; this is moved to ExecBase at task's startup
  2271.     move.b    #-1,(TC_IDNESTCNT,A4)
  2272.     move.b    #-1,(TC_TDNESTCNT,A4)
  2273.  
  2274.     tst.w    (TC_SWITCH,A4)
  2275.     beq.b    .nosw
  2276.     or.b    #TF_SWITCH,(TC_FLAGS,A4)
  2277. .nosw
  2278.     tst.w    (TC_LAUNCH,A4)
  2279.     beq.b    .noln
  2280.     or.b    #TF_LAUNCH,(TC_FLAGS,A4)
  2281. .noln
  2282.     move.l    (TC_SPUPPER,A4),D0
  2283.     sub.l    #TSV_SIZE,D0        ; allocates stack space to save information
  2284.     move.l    D0,(TC_SPREG,A4)    ; SP register initialize (this is not the real
  2285.                     ; SP register but rather SP-TSV_SIZE)
  2286.  
  2287.     ; init tc_MemList if zero
  2288.  
  2289.     tst.l    (TC_MEMENTRY,A4)
  2290.     bne.b    .memlist_set
  2291.     lea    (TC_MEMENTRY,A4),A1
  2292.     INITLIST    A1
  2293. .memlist_set
  2294.  
  2295.     move.l    (TC_SPREG,A4),A1
  2296.  
  2297.     ; fills in some values (start and end of the task)
  2298.  
  2299.     move.l    A2,(TSV_CURRENTPC,A1)
  2300.     move.l    A3,(TSV_ENDPC,A1)
  2301.  
  2302.     ; insert the task in exec task list
  2303.  
  2304.     JSRLIB    Disable
  2305.  
  2306.     move.b    #TS_READY,(TC_STATE,A4)    ; mark as ready to run
  2307.     lea    (TaskReady,A6),A0    ; TaskReady list
  2308.     move.l    A4,A1            ; task node
  2309.     JSRLIB    Enqueue            ; inserts into exec TaskReady list
  2310.  
  2311.     ; task priorities not supported yet
  2312.  
  2313.     JSRLIB    Enable
  2314.  
  2315.     movem.l    (A7)+,D2/A4
  2316.     RTS
  2317.  
  2318. .defaultname:
  2319.     dc.b    "OSEmu task",0
  2320.     even
  2321.  
  2322. .fail:
  2323.     EMUFAIL    _LVOAddTask,_execname
  2324.     endif
  2325.  
  2326. ; FindTask
  2327. ; < A1: must be NULL or else unsupported
  2328. ; > D0: task structure
  2329.  
  2330. _FindTask:
  2331.         MOVE.L    A1,-(A7)    ;fails if not own task searched
  2332.         TST.L    (A7)+
  2333.         BNE.S    .FAIL
  2334.         MOVE.L    4.W,A0
  2335.         MOVE.L    ThisTask(A0),D0
  2336.         RTS
  2337.  
  2338. .FAIL
  2339.         pea    _LVOFindTask
  2340.         pea    _execname
  2341.         bra    _emufail
  2342.  
  2343. **************************************************************************
  2344. *   MEMORYFUNCTIONS                                                      *
  2345. **************************************************************************
  2346.  
  2347. ; AllocEntry(): Added by JOTD by request of Bored Seal
  2348. ; Tested on DragonNinja
  2349.  
  2350. ; < A0: memList
  2351. ; > D0: allocated memList
  2352.  
  2353. ALLOCENTRY:
  2354.     movem.l    D1-A6,-(A7)
  2355.     move.l    A0,A5
  2356.     moveq.l    #0,D0
  2357.     move.w    LN_SIZE(A5),D0
  2358.     lsl.l    #3,D0
  2359.     add.l    #$10,D0
  2360.     move.l    D0,D5        ; size
  2361.     move.l    #MEMF_PUBLIC,D1
  2362.     jsr    (_LVOAllocMem,a6)
  2363.     move.l    D0,D6
  2364.     beq.b    .fail
  2365.  
  2366.     ; allocated OK
  2367.  
  2368.     move.l    D6,A4
  2369.  
  2370.     moveq.l    #0,D0
  2371.     move.l    A5,A0
  2372.     move.l    A4,A1
  2373.     move.l    D5,D0
  2374.     jsr    (_LVOCopyMem,a6)
  2375.  
  2376.     moveq.l    #0,D2
  2377.     move.w    LN_SIZE(A4),D2    ; # of items
  2378.     
  2379.     add.l    #LN_SIZE+2,A4
  2380. .loop
  2381.     move.l    (A4)+,D1    ; flags
  2382.     move.l    (A4)+,D0    ; size
  2383.     jsr    (_LVOAllocMem,a6)
  2384.     beq.b    .fail        ; alloc problem
  2385.     move.l    D0,-8(A4)    ; store memblock start in flags (union)
  2386.     subq.l    #1,D2
  2387.     bne.b    .loop
  2388.  
  2389.     move.l    D6,D0        ; new memlist
  2390. .exit
  2391.     movem.l    (A7)+,D1-A6
  2392.     rts
  2393.  
  2394. .fail:
  2395.     moveq.l    #0,D0
  2396.     bset    #31,D0
  2397.     bra.b    .exit
  2398.  
  2399. ; FreeEntry(): still untested but should work
  2400. ; JOTD: it does NOT
  2401.  
  2402. FREEENTRY:
  2403.  
  2404.     movem.l    D1-A6,-(A7)
  2405.     move.l    A0,A4
  2406.  
  2407.     moveq.l    #0,D2        ; JOTD: this would help
  2408.  
  2409.     move.w    LN_SIZE(A4),D2    ; # of items
  2410.     beq.b    .exit        ; JOTD: and this too
  2411.     
  2412.     add.l    #LN_SIZE+2,A4
  2413. .loop
  2414.     move.l    (A4)+,A1    ; memory pointer
  2415.     move.l    (A4)+,D0    ; memory size
  2416.     jsr    (_LVOFreeMem,a6)
  2417.     subq.l    #1,D2
  2418.     bne.b    .loop
  2419.  
  2420. .exit
  2421.     movem.l    (A7)+,D1-A6
  2422.     rts
  2423.  
  2424. _COPYMEM
  2425.     TST.L    D0
  2426.     beq.b    .EXIT
  2427.     CMP.L    A0,A1
  2428.     BLS.S    .ASCEND
  2429. .DESCEND
  2430.     LEA.L    (A0,D0.L),A0
  2431.     LEA.L    (A1,D0.L),A1
  2432. .LD    MOVE.B    -(A0),-(A1)
  2433.     SUBQ.L    #1,D0
  2434.     BNE.S    .LD
  2435.     RTS
  2436.  
  2437. .ASCEND
  2438. .LA    MOVE.B    (A0)+,(A1)+
  2439.     SUBQ.L    #1,D0
  2440.     BNE.S    .LA
  2441. .EXIT
  2442.     RTS
  2443.  
  2444. _COPYMEMQUICK
  2445.     tst.l    D0
  2446.     beq.b    .EXIT
  2447.     CMP.L    A0,A1
  2448.     BLS.S    .ASCEND
  2449. .DESCEND
  2450.     LEA.L    (A0,D0.L),A0
  2451.     LEA.L    (A1,D0.L),A1
  2452. .LD    MOVE.L    -(A0),-(A1)
  2453.     SUBQ.L    #4,D0
  2454.     BNE.S    .LD
  2455.     RTS
  2456.  
  2457. .ASCEND
  2458. .LA    MOVE.L    (A0)+,(A1)+
  2459.     SUBQ.L    #4,D0
  2460.     BNE.S    .LA
  2461. .EXIT
  2462.     RTS
  2463.  
  2464. ; FreeMem()
  2465. ; < D0: size
  2466. ; < A1: pointer
  2467.  
  2468. _FreeMem:
  2469.     TST.L    D0
  2470.     BNE.S    .NOZERO
  2471.     RTS
  2472.  
  2473. .NOZERO
  2474.     cmp.l    #$BBBBBBBB,A1    ; temporary, Wings
  2475.     bne    .sk
  2476.     rts
  2477. .sk
  2478.     cmp.l    #$1000000,D0    ; JOTD: safety, I had to switch off my amy
  2479.     bcc.b    .FREEMFAIL    ; when the value passed was too high (here >16MB)!    
  2480.  
  2481.     CMP.L    #ALLOCMTAB,A1        ; above ALLOCMTAB
  2482.     BHS.S    .OK            ; okay, try to free
  2483. .FREEMFAIL
  2484.     EMUFAIL    _LVOFreeMem,_execname
  2485.  
  2486. .OK
  2487.     tst.l    OSM_EXPMEM
  2488.     beq.b    .FreeChip
  2489.  
  2490.     cmp.l    OSM_EXPMEM,A1
  2491.     bcs.b    .FreeChip    ; below expansion memory -> chipmem
  2492.  
  2493.     ; fast memory - added by JOTD
  2494.  
  2495. .FreeFast:
  2496.     move.l    OSM_EXPMEM,A0        ; start of memory expansion
  2497.     add.l    OSM_EXPSIZE,A0        ; size of memory expansion
  2498.  
  2499.     cmp.l    A0,A1
  2500.     bcc.b    .FREEMFAIL        ; trying to free above memory expansion
  2501.  
  2502.     move.l    OSM_EXPMEM,A0        ; start of memory table (fastmem)
  2503.     sub.l    A0,A1            ; corrects offset on address !!
  2504.     bra.b    .FreeGeneric        ; removed by Harry
  2505.  
  2506.     ; chip memory - Harry's original
  2507.  
  2508. .FreeChip:
  2509.     LEA.L    ALLOCMTAB,A0        ; start of memory table (chipmem)
  2510.  
  2511. .FreeGeneric:
  2512.     bsr    GetAllocSem
  2513.  
  2514.     MOVEM.L    D2/D3/A2/A3,-(A7)
  2515.     MOVE.L    A1,D1
  2516.     ADDQ.L    #7,D0
  2517.     AND.L    #$FFFFFFF8,D1
  2518.     AND.L    #$FFFFFFF8,D0
  2519.     BEQ.S    .QU
  2520.     LSR.L    #3,D0            ;# OF BITS TO CLEAR IN D0
  2521.  
  2522.                     ;FREE MEM NOT ON A $40-BOUNDARY
  2523.                     ;EVAL BEGINBYTE
  2524.     MOVE.L    D1,D2
  2525.     LSR.L    #6,D2
  2526.     LEA.L    (A0,D2.L),A0
  2527.     MOVEM.L    D1,-(A7)
  2528.     AND.L    #$3F,D1
  2529.     BEQ.S    .CNTCLR
  2530.     LSR.L    #3,D1
  2531. .2    BSET    D1,(A0)
  2532.     ADDQ.L    #1,D1
  2533.     ADDQ.L    #8,(A7)
  2534.     SUBQ.L    #1,D0
  2535.     BEQ.S    .1
  2536.     CMP.L    #8,D1
  2537.     BEQ.S    .CNTCLR2
  2538.     BRA.S    .2
  2539.  
  2540. .1    MOVEM.L    (A7)+,D1
  2541.     BRA.S    .QU
  2542.  
  2543. .CNTCLR2
  2544.     ADDQ.L    #1,A0
  2545. .CNTCLR
  2546.     MOVEM.L    (A7)+,D1
  2547.  
  2548.                     ;CLEAR $40-BOUNDARY-BYTES
  2549. .3    CMP.L    #8,D0
  2550.     BLO.S    .CLREND
  2551.  
  2552.     ST    (A0)+
  2553.     SUBQ.L    #8,D0
  2554.     BRA.S    .3
  2555.  
  2556.  
  2557. .CLREND                    ;CLEAR END NOT ENDING ON A $40-
  2558.     TST.L    D0            ;BOUNDARY
  2559.     BEQ.S    .QU
  2560.     SUBQ.L    #1,D0
  2561.     BSET    D0,(A0)
  2562.     BRA.S    .CLREND
  2563.  
  2564. .QU    MOVEM.L    (A7)+,D2/D3/A2/A3
  2565.  
  2566.     bsr    FreeAllocSem
  2567.     RTS
  2568.  
  2569.  
  2570. ; < A1: memory pointer
  2571. ; > D0: flags
  2572.  
  2573. TYPEOFMEM:
  2574.     move.l    A0,-(A7)
  2575.     moveq.l    #0,D0
  2576.     cmp.l    MaxLocMem(A6),A1
  2577.     bcs.b    .chipmem
  2578.  
  2579.     move.l    OSM_EXPMEM,A0
  2580.     cmp.l    A0,A1
  2581.     bcs.b    .exit        ; not in chip and below expansion: unknown
  2582.  
  2583.     add.l    OSM_EXPSIZE,A0
  2584.     cmp.l    A0,A1
  2585.     bcc.b    .exit        ; not in chip and above expansion: unknown
  2586.  
  2587.     move.l    #MEMF_FAST,D0
  2588.     bra.b    .exit
  2589.  
  2590. .chipmem
  2591.     cmp.l    #$1000,A1
  2592.     bcs.b    .exit
  2593.     move.l    #MEMF_CHIP,D0
  2594. .exit
  2595.     move.l    (A7)+,A0
  2596.     rts
  2597.  
  2598.  
  2599. ;>D0:SIZE
  2600. ;>D1:CONDITIONS
  2601. ;<D0:ADDY
  2602.  
  2603. _AllocVec:
  2604.     move.l    D2,-(A7)
  2605.     addq.l    #$4,D0    ; adds bytes to store the size
  2606.     move.l    D0,D2    ; saves size
  2607.  
  2608.     bsr    _AllocMem
  2609.     tst.l    D0
  2610.     beq.b    .exit
  2611.  
  2612.     move.l    D0,A0    
  2613.     move.l    D2,(A0)        ; save size at start of block
  2614.  
  2615.     ; adds $4 to base address
  2616.  
  2617.     addq.l    #$4,D0
  2618. .exit:
  2619.     move.l    (A7)+,D2
  2620.     rts
  2621.  
  2622. ;>A1:Start of block
  2623.  
  2624. _FreeVec:
  2625.     move.l    -(A1),D0    ; gets block length
  2626.     JMPLIB    FreeMem
  2627.  
  2628. ;>D0:SIZE
  2629. ;>D1:CONDITIONS
  2630. ;<D0:ADDY
  2631. ;LIMITATIONS:
  2632. ;        -MEMORY IS $40-ALIGNED (SOME BYTES WASTED)
  2633. ;        -MEMF_LARGEST IS IGNORED AS IN THE OS
  2634. ;        -ATM, ALSO MEMF_REVERSE IS IGNORED
  2635.  
  2636. _AllocMem:
  2637.     tst.l    d0
  2638.     beq    .nomem    ; not fatal anymore, allows broken programs to work
  2639.  
  2640.     MOVE.L    D1,-(A7)
  2641.     and.l    #~(MEMF_REVERSE!MEMF_LARGEST!MEMF_FAST!MEMF_CHIP!MEMF_CLEAR!MEMF_PUBLIC),(A7)+
  2642. ;;    bne    .fail        ; wrong flags
  2643.                 ; removed (Test Drive II allocates memory with garbage in D1)
  2644.  
  2645.     btst    #MEMB_CHIP,d1    ; chipmem required?
  2646.     BNE.S    .NORMALCHIP
  2647.  
  2648.     ; JOTD: MEMB_CHIP not specified -> try to alloc fast
  2649.  
  2650.     movem.l    D0/D1,-(A7)
  2651.     bsr.b    .FMEM
  2652.     tst.l    D0
  2653.     bne.b    .fastok
  2654.     movem.l    (A7)+,D0/D1
  2655.     bra.b    .NORMALCHIP    ; could not allocate fastmem: try chipmem
  2656. .fastok:
  2657.     addq.l    #8,A7
  2658.     rts
  2659.  
  2660. .NORMALCHIP
  2661.     MOVEM.L    D2-D5/A2/A3/A4,-(A7)
  2662.  
  2663.     addq.l    #7,d0
  2664.     LSR.L    #3,D0
  2665.     ;SEARCH SUFFICIENT BIG MEMAREA
  2666.  
  2667.     LEA.L    ALLOCMTAB,A0    ;FIND A BYTE != 0 (FREE MEM)
  2668.     move.l    ALLOCMTABSIZE,A3
  2669.     sub.l    A4,A4
  2670.  
  2671. .doalloc
  2672.     bsr    AllocGeneric
  2673.  
  2674.  
  2675.     MOVEM.L    (A7)+,D2-D5/A2/A3/A4
  2676.     tst.l    D0
  2677.     RTS
  2678.  
  2679. .FMEM:
  2680.     tst.l    OSM_EXPMEM
  2681.     bne.b    .ALLOCFAST        ; expansion available: allocate
  2682.  
  2683.     ; no expansion: check if fast is required
  2684.  
  2685.     btst    #MEMB_FAST,d1
  2686.     bne.b    .nomem            ; fastmem not available: error
  2687.  
  2688.     bra.b    .NORMALCHIP        ; no fast memory configured: try chipmem
  2689.  
  2690. .ALLOCFAST
  2691.     MOVEM.L    D2-D5/A2/A3/A4,-(A7)
  2692.     addq.l    #7,d0
  2693.     LSR.L    #3,D0
  2694.     ;SEARCH SUFFICIENT BIG MEMAREA
  2695.  
  2696.     move.l    OSM_EXPMEM,A0    ;FIND A BYTE != 0 (FREE MEM)
  2697.     move.l    ALLOCFASTMTABSIZE,A3
  2698.     move.l    A0,A4        ; offset to add for ALLOCA
  2699.  
  2700.     bra    .doalloc
  2701.  
  2702. .nomem:
  2703.     moveq.l    #0,D0
  2704.     rts
  2705.  
  2706. .fail    
  2707.     EXECFAIL _LVOAllocMem
  2708.  
  2709. ; Added by JOTD to be used with chip and fast alloc
  2710. ; < A0: alloc table
  2711. ; < A3: end of alloc table
  2712. ; < A4: offset to add to calculated address (0 for chip, OSM_EXPMEM for fast)
  2713.  
  2714. AllocGeneric:
  2715.     bsr    GetAllocSem
  2716.  
  2717.     MOVEQ.L    #0,D4
  2718. .2
  2719.     CMP.B    #$FF,(A0)+
  2720.     BEQ.S    .1
  2721.     ADD.L    #$40,D4
  2722.     CMP.L    A3,A0
  2723.     BNE.S    .2
  2724.     BRA.S    .ERR
  2725.                     ;found free mem
  2726.                     ;CHECK NOW SIZE 
  2727. .1    SUBQ.L    #1,A0
  2728.                     ;D4-BASE
  2729. .3
  2730.     MOVE.L    D0,-(A7)        ;WANTED SIZE
  2731.     MOVE.L    D4,-(A7)        ;FOUND ADDRESS
  2732.  
  2733. .6    TST.L    D0
  2734.     BMI.S    .CHKEND
  2735.     CMP.B    #$FF,(A0)
  2736.     BNE.S    .TOOSMALL
  2737.     ADDQ.L    #1,A0        ;CHECK FOR EXCEEDING THE SIZE OF ALLOCMTAB
  2738.                 ;MAY BE OMITTED SINCE ITS END CONTAINS $0
  2739.     ADD.L    #$40,D4
  2740.     SUBQ.L    #8,D0
  2741.     BEQ.S    .CHKEND
  2742.     BRA.S    .6
  2743.  
  2744. .TOOSMALL                ;LATER TODO: FIND EVENTUAL ZEROBITS
  2745.  
  2746.     ADDQ.L    #4,A7            ;DISCARD D4 ON STACK
  2747.     MOVE.L    (A7)+,D0
  2748.     ADDQ.L    #1,A0
  2749. ;    AND.L    #$FFFFFFC0,D4
  2750.     ADD.L    #$40,D4
  2751.     BRA.S    .2
  2752.  
  2753. .CHKEND
  2754.     MOVE.L    (A7)+,D4
  2755.     MOVE.L    (A7)+,D0
  2756.     LSL.L    #3,D0
  2757. ;D0-SIZE
  2758. ;D4-ABS
  2759.     ;MARK AREA AS USED
  2760.     MOVE.L    D4,A1
  2761.     MOVE.L    D0,D2
  2762.  
  2763.     add.l    A4,A1            ; added by JOTD, offset for fastmem
  2764.     BSR.W    PROTECTED_ALLOCA
  2765.                     ;MEMORYSPACE ALREADY SUCESSFULLY
  2766.                     ;CHECKED, RETURNCODE MAY BE IGNORED
  2767.  
  2768.     ; JOTD: check if MEMB_CLEAR is set, if not, don't clear memory
  2769.     ; (increases performance)
  2770.  
  2771.     btst    #MEMB_CLEAR,D1
  2772.     beq.b    .exit
  2773.  
  2774.     MOVE.L    A1,A2
  2775. ;    MOVE.L    D4,D2
  2776.     LSR.L    #2,D2
  2777.  
  2778.     BRA.S    .CLRM
  2779.  
  2780. .CLRM1    CLR.L    (A2)+
  2781. .CLRM    DBF    D2,.CLRM1
  2782. .exit
  2783.     bsr    FreeAllocSem
  2784.     tst.l    d0            ;some progs expect an correct set zero flag
  2785.     rts
  2786. .ERR:
  2787.     moveq.l    #0,D0            ; unable to allocate: table full
  2788.     bra.b    .exit
  2789.  
  2790. ; JOTD: semaphores needed for AllocMem/FreeMem/AllocAbs stuff
  2791.  
  2792. GetAllocSem:
  2793.     tst.w    _Multitasks
  2794.     beq.b    .skip
  2795.     movem.l    D0-D1/A0-A1,-(A7)
  2796.     JSRLIB    Forbid
  2797.     movem.l    (A7)+,D0-D1/A0-A1
  2798. .skip
  2799.     rts
  2800.  
  2801. FreeAllocSem:
  2802.     tst.w    _Multitasks
  2803.     beq.b    .skip
  2804.     movem.l    D0-D1/A0-A1,-(A7)
  2805.     JSRLIB    Permit
  2806.     movem.l    (A7)+,D0-D1/A0-A1
  2807. .skip
  2808.     rts
  2809.  
  2810. ;RETURNS AT THE MOMENT ONLY THE SIZE OF THE LARGEST CHUNK-$1000 SAFETY
  2811.  
  2812. _AvailMem:
  2813.     MOVEM.L    A2/A3/D2/D3,-(A7)
  2814.     MOVE.L    D1,D2
  2815.     AND.L    #MEMF_FAST,D2
  2816.     BNE.S    .FMEM
  2817.  
  2818.     ; check for chipmem available
  2819.  
  2820.     LEA.L    ALLOCMTAB,A0
  2821.     move.l    ALLOCMTABSIZE(pc),A3
  2822.  
  2823.     bsr    GenericAvail
  2824.  
  2825.  
  2826. .exit
  2827.     MOVEM.L    (A7)+,A2/A3/D2/D3
  2828.     RTS
  2829.  
  2830. .FMEM    
  2831.     move.l    OSM_EXPMEM,A0
  2832.     cmp.l    #0,A0
  2833.     beq.b    .nomem
  2834.     move.l    ALLOCFASTMTABSIZE(pc),A3
  2835.  
  2836.     bsr    GenericAvail
  2837.     bra.b    .exit
  2838.  
  2839. .nomem:
  2840.     moveq.l    #0,D0
  2841.     bra.b    .exit
  2842.     
  2843. ; for chip & fast memory
  2844.  
  2845. GenericAvail:
  2846.     MOVEQ.L    #0,D2
  2847.     MOVEQ.L    #0,D3
  2848. .3    MOVE.B    (A0)+,D0
  2849.     CMP.B    #$FF,D0            ;SEARCH CHUNK
  2850.     BNE.S    .1
  2851.     ADD.L    #$40,D3
  2852.     BRA.S    .2
  2853.  
  2854. .1    CMP.L    D2,D3            ;END OF CHUNK, COMPARE SIZE WITH
  2855.     BLO.S    .4            ;PREVIOUS SIZE
  2856.     MOVE.L    D3,D2            ;ACTUAL IS LARGER -> NEW LARGEST SIZE
  2857. .4    MOVEQ.L    #0,D3            ;RESET SIZE OF NEXT CHUNK
  2858. .2    CMP.L    A3,A0            ;UNTIL MEMORYTABLE EXCEEDED
  2859.     BLO.S    .3
  2860.     cmp.l    d2,d3
  2861.     blo    .5
  2862.     move.l    d3,d2
  2863. .5
  2864.     CMP.L    #$2000,D2
  2865.     BLO.S    .6
  2866.     SUB.L    #$1000,D2
  2867. .6    MOVE.L    D2,D0
  2868.     rts
  2869.  
  2870. _AllocAbs:
  2871.     bsr    GetAllocSem
  2872.     bsr    PROTECTED_ALLOCA
  2873.     bsr    FreeAllocSem
  2874.     tst.l    D0
  2875.     rts
  2876.  
  2877. PROTECTED_ALLOCA:
  2878.     tst.l    OSM_EXPMEM
  2879.     beq.b    .ALLOCA_CHIP    ; no expansion: alloc chipmem
  2880.     cmp.l    OSM_EXPMEM,A1
  2881.     bcc.b    .ALLOCA_FAST    ; above expansion: fast memory
  2882. .ALLOCA_CHIP:
  2883.     movem.l    A4,-(A7)
  2884.     LEA.L    ALLOCMTAB,A4    ; chipmem alloc table
  2885.     bsr    GenericAllocAbs
  2886.     movem.l    (A7)+,A4
  2887.     move.l    D0,A1
  2888.     rts
  2889.  
  2890. .ALLOCA_FAST:
  2891.     movem.l    A4,-(A7)
  2892.     move.l    OSM_EXPMEM,A4        ; fastmem alloc table
  2893.     sub.l    A4,A1            ; substracts expbase from the address
  2894.     bsr    GenericAllocAbs        ; the routine returns only offset
  2895.     add.l    A4,D0            ; relative to memory block so we have to add expbase
  2896.     move.l    D0,A1            ; A1=D0 for Harry!
  2897.     movem.l    (A7)+,A4
  2898.     rts
  2899.  
  2900. ; < A4: allocmtab (added by JOTD to be used by fastmem and chipmem)
  2901.  
  2902.  
  2903. GenericAllocAbs
  2904.  
  2905.     MOVEM.L    D1/D2/D3/A2/A3,-(A7)
  2906.     MOVE.L    A7,A3        ; save stack
  2907.     MOVE.L    _Slave(PC),A2
  2908.     MOVE.L    A1,D2
  2909.     ADD.L    D0,D2
  2910.     CMP.L    A2,D2
  2911.     BHS.W    .ERR
  2912.  
  2913.     move.l    A1,D1
  2914.  
  2915. ;    ADDQ.L    #7,D1
  2916.     AND.L    #$FFFFFFF8,D1
  2917.     AND.L    #$FFFFFFF8,D0
  2918.     BEQ.W    .QU
  2919.     MOVEM.L    D0/D1,-(A7)
  2920.  
  2921.     LSR.L    #3,D0            ;# OF BITS TO CLEAR IN D0
  2922.  
  2923.                     ;CHECK MEM NOT ON A $40-BOUNDARY
  2924.                     ;EVAL BEGINBYTE
  2925.     MOVE.L    D1,D2
  2926.     LSR.L    #6,D2
  2927.     move.L    A4,A0
  2928.     LEA.L    (A0,D2.L),A0
  2929.     MOVEM.L    D1,-(A7)
  2930.     AND.L    #$3F,D1
  2931.     BEQ.S    .CNTCHK
  2932.     LSR.L    #3,D1
  2933. .2    BTST    D1,(A0)
  2934.     BEQ.W    .ERR
  2935.     ADDQ.L    #1,D1
  2936. ;    ADDQ.L    #8,(A7)
  2937.     SUBQ.L    #1,D0
  2938.     BEQ.S    .1
  2939.     CMP.L    #8,D1
  2940.     BEQ.S    .CNTCHK2
  2941.     BRA.S    .2
  2942.  
  2943. .1    MOVEM.L    (A7)+,D1
  2944.     BRA.S    .CONT
  2945.  
  2946. .CNTCHK2
  2947.     ADDQ.L    #1,A0
  2948. .CNTCHK
  2949.     MOVEM.L    (A7)+,D1
  2950.  
  2951.                     ;CLEAR $40-BOUNDARY-BYTES
  2952. .3    CMP.L    #8,D0
  2953.     BLO.S    .CLREND
  2954.  
  2955.     CMP.B    #$FF,(A0)+
  2956.     BNE.S    .ERR
  2957.     SUBQ.L    #8,D0
  2958.     BRA.S    .3
  2959.  
  2960.  
  2961. .CLREND
  2962.     TST.L    D0
  2963.     BEQ.S    .CONT
  2964.     SUBQ.L    #1,D0
  2965.     BTST    D0,(A0)
  2966.     BEQ.S    .ERR
  2967.     BRA.S    .CLREND
  2968.  
  2969. .CONT    MOVEM.L    (A7)+,D0/D1
  2970.  
  2971.                     ;ALLOC MEM
  2972.  
  2973.     LSR.L    #3,D0            ;# OF BITS TO CLEAR IN D0
  2974.  
  2975.                     ;MARK MEM NOT ON A $40-BOUNDARY
  2976.                     ;EVAL BEGINBYTE
  2977.     MOVE.L    D1,D2
  2978.     LSR.L    #6,D2
  2979.     move.l    A4,A0
  2980.     LEA.L    (A0,D2.L),A0
  2981.     MOVEM.L    D1,-(A7)
  2982.     AND.L    #$3F,D1
  2983.     BEQ.S    .CNTMARK
  2984.     LSR.L    #3,D1
  2985. .22    BCLR    D1,(A0)
  2986.     ADDQ.L    #1,D1
  2987. ;    ADDQ.L    #8,(A7)
  2988.     SUBQ.L    #1,D0
  2989.     BEQ.S    .21
  2990.     CMP.L    #8,D1
  2991.     BEQ.S    .CNTMARK2
  2992.     BRA.S    .22
  2993.  
  2994. .21    MOVEM.L    (A7)+,D1
  2995.     BRA.S    .QU
  2996.  
  2997. .CNTMARK2
  2998.     ADDQ.L    #1,A0
  2999. .CNTMARK
  3000.     MOVEM.L    (A7)+,D1
  3001.  
  3002.                     ;CLEAR $40-BOUNDARY-BYTES
  3003. .23    CMP.L    #8,D0
  3004.     BLO.S    .MARKEND
  3005.  
  3006.     SF    (A0)+
  3007.     SUBQ.L    #8,D0
  3008.     BRA.S    .23
  3009.  
  3010.  
  3011. .MARKEND
  3012.     TST.L    D0
  3013.     BEQ.S    .QU
  3014.     SUBQ.L    #1,D0
  3015.     BCLR    D0,(A0)
  3016.     BRA.S    .MARKEND
  3017.  
  3018. .QU
  3019.  
  3020.     MOVE.L    D1,D0
  3021. .out
  3022.     MOVE.L    A3,A7        ; restore stack
  3023.     MOVEM.L    (A7)+,D1/D2/D3/A2/A3
  3024.     RTS
  3025. .ERR2
  3026. .ERR
  3027.     MOVEQ.L    #0,D0
  3028.     bra.b    .out
  3029.  
  3030. ****************************************************************************
  3031. * LIST HANDLING IN EXEC                                                    *
  3032. ****************************************************************************
  3033.  
  3034. ; Enqueue, was disabled, JOTD reactivated it
  3035.  
  3036. _Enqueue    ;a0=list a1=node
  3037.     MOVE.B    (LN_PRI,A1),D1
  3038.     MOVE.L    (LN_SUCC,A0),D0
  3039. .1908    MOVE.L    D0,A0
  3040.     MOVE.L    (LN_SUCC,A0),D0
  3041.     BEQ.S     .1914
  3042.     CMP.B     (LN_PRI,A0),D1
  3043.     BLE.S     .1908
  3044. .1914    MOVE.L    (LN_PRED,A0),D0
  3045.     MOVE.L    A1,(LN_PRED,A0)
  3046.     MOVE.L    A0,(LN_SUCC,A1)
  3047.     MOVE.L    D0,(LN_PRED,A1)
  3048.     MOVE.L    D0,A0
  3049.     MOVE.L    A1,(LN_SUCC,A0)
  3050.     RTS
  3051.  
  3052. ; FindPort
  3053. ; < A1: name
  3054. ; > D0: port
  3055.  
  3056. _FINDPORT
  3057.     lea    (PortList,A6),A0    ; system port list
  3058.     bra.s    _FINDNAME
  3059.  
  3060. _FINDNAME    ;a0-list, a1-name, returns d0-node
  3061.     MOVEM.L    A2,-(A7)
  3062.     MOVE.L    A0,A2        ;find listheader (lords of war *grr*)
  3063. .HSEARCH
  3064.     TST.L    4(A2)
  3065.     BEQ.S    .HFOUND
  3066.     MOVE.L    4(A2),A2
  3067.     BRA.S    .HSEARCH
  3068.  
  3069. .HFOUND
  3070. .LOOP    TST.L    (A2)
  3071.     BEQ.S    .NOTFOUND
  3072.     MOVE.L    (A2),A2
  3073.     MOVE.L    LN_NAME(A2),A0
  3074.     BSR.W    _strcmp
  3075.     TST.L    D0
  3076.     BEQ.S    .FOUND
  3077.     BRA.S    .LOOP
  3078.  
  3079. .FOUND    MOVE.L    A2,D0
  3080. .exit
  3081.     MOVEM.L    (A7)+,A2
  3082.     TST.L    D0
  3083.     RTS
  3084.  
  3085. .NOTFOUND
  3086.     MOVEQ.L    #0,D0
  3087.     bra.b    .exit
  3088.  
  3089. _INSERT        ;a0-list a1-node to be inserted a2-node after which to insert
  3090. .HSEARCH
  3091.     TST.L    4(A0)
  3092.     BEQ.S    .HFOUND
  3093.     MOVE.L    4(A0),A0
  3094.     BRA.S    .HSEARCH
  3095.  
  3096. .HFOUND
  3097.     MOVE.L    A2,-(A7)
  3098.     TST.L    (A7)+
  3099.     BEQ.S    .ADDHEAD
  3100.     CMP.L    A0,A2
  3101.     BEQ.S    .ADDHEAD
  3102.     MOVE.L    LH_TAILPRED(A0),-(A7)
  3103.     CMPM.L    (A7)+,(A2)+    ;IF INSERTING AT END -> .ADDTAIL
  3104.     LEA.L    -4(A2),A2
  3105.     BEQ.S    .ADDTAIL
  3106.     MOVE.L    (A2),A0        ;SUCCESSOR
  3107.     MOVE.L    A1,(LN_PRED,A0)    ;NEW PREDECESSOR
  3108.     MOVE.L    (A2),(A1)    ;SUCCESSOR IN ELEMENT TO BE INSERTED
  3109.     MOVE.L    A2,(LN_PRED,A1)    ;PREDECESSOR IN ELEMENT TO BE INSERTED
  3110.     MOVE.L    A1,(A2)        ;INSERTED NODE IS SUCCESSOR
  3111.     RTS
  3112.  
  3113. .ADDTAIL
  3114.     MOVE.L    (A2),(A1)
  3115.     MOVE.L    A2,LN_PRED(A1)
  3116.     MOVE.L    A1,LH_TAILPRED(A0)
  3117.     MOVE.L    A1,(A2)
  3118.     RTS
  3119.  
  3120. .ADDHEAD
  3121.     bra    _ADDHEAD
  3122.  
  3123. ; < A0: list
  3124. ; > D0: node
  3125. ; Uses commodore includes macro (exec/lists.i)
  3126. _REMHEAD:
  3127.     movem.l    A1,-(A7)
  3128.     REMHEAD
  3129.     movem.l    (A7)+,A1
  3130.     RTS
  3131.  
  3132. _ADDTAIL:
  3133.     movem.l    D0/A0,-(A7)
  3134.     ADDTAIL
  3135.     movem.l    (A7)+,D0/A0
  3136.     RTS
  3137.  
  3138. _ADDHEAD:
  3139.     movem.l    D0/A0,-(A7)
  3140.     ADDHEAD
  3141.     movem.l    (A7)+,D0/A0
  3142.     RTS
  3143.  
  3144.  
  3145. ; < A0: list
  3146. ; > D0: node
  3147. ; Uses commodore includes macro (exec/lists.i)
  3148. _REMTAIL:
  3149.     movem.l    A1,-(A7)
  3150.     REMTAIL
  3151.     movem.l    (A7)+,A1
  3152.     RTS
  3153.  
  3154. _REMOVE
  3155.     movem.l    A0/A1,-(A7)
  3156.     REMOVE
  3157.     movem.l    (A7)+,A0/A1
  3158.     RTS
  3159.  
  3160.     ifeq 1
  3161. _ADDHEAD
  3162.     MOVE.L    A2,-(A7)
  3163.     MOVE.L    (A0),A2
  3164.     TST.L    (A2)
  3165.     BEQ.S    .EMPTYLIST
  3166.  
  3167.     MOVE.L    (A0),A2
  3168.     MOVE.L    A2,(A1)
  3169.     MOVE.L    LN_PRED(A2),LN_PRED(A1)
  3170.     MOVE.L    A1,LN_PRED(A2)
  3171.     MOVE.L    A1,(A0)
  3172.     BRA.S    .END
  3173.  
  3174. .EMPTYLIST
  3175.     MOVE.L    (A0),(A1)
  3176.     MOVE.L    LH_TAILPRED(A0),LN_PRED(A1)
  3177.     MOVE.L    A1,(A0)            ;INSERT ELEMENT AS FIRST INTO *LIST
  3178.     MOVE.L    A1,LH_TAILPRED(A0)    ;ELEMENT IS ALSO LAST ONE
  3179. .END    MOVE.L    (A7)+,A2
  3180.     RTS
  3181.  
  3182. _ADDTAIL
  3183.     MOVE.L    A2,-(A7)
  3184.     MOVE.L    (A0),A2
  3185.     TST.L    (A2)
  3186.     BEQ.S    .EMPTYLIST
  3187.     MOVE.L    LH_TAILPRED(A0),A2
  3188.  
  3189.     MOVE.L    (A2),(A1)
  3190.     MOVE.L    A2,LN_PRED(A1)
  3191.     MOVE.L    A1,LH_TAILPRED(A0)
  3192.     MOVE.L    A1,(A2)
  3193.     BRA.S    .END
  3194.  
  3195. .EMPTYLIST
  3196.     MOVE.L    (A0),(A1)
  3197.     MOVE.L    LH_TAILPRED(A0),LN_PRED(A1)
  3198.     MOVE.L    A1,(A0)            ;INSERT ELEMENT AS FIRST INTO *LIST
  3199.     MOVE.L    A1,LH_TAILPRED(A0)
  3200. .END    MOVE.L    (A7)+,A2
  3201.     RTS
  3202.  
  3203. _REMOVE        ;A1-NODE TO REMOVE
  3204.     MOVEM.L    A2,-(A7)
  3205.     MOVE.L    A1,A2
  3206. .HSEARCH
  3207.     TST.L    4(A2)
  3208.     BEQ.S    .HFOUND
  3209.     MOVE.L    4(A2),A2
  3210.     BRA.S    .HSEARCH
  3211.  
  3212. .HFOUND
  3213.     MOVE.L    (A1),A0
  3214.     TST.L    (A0)
  3215.     BEQ.S    .REMTAIL
  3216.     CMP.L    (A2),A1
  3217.     BEQ.S    .REMHEAD
  3218.  
  3219.     MOVE.L    LN_PRED(A1),A0
  3220.     MOVE.L    (A1),(A0)
  3221.     MOVE.L    (A1),A0
  3222.     MOVE.L    LN_PRED(A1),LN_PRED(A0)
  3223. .END    MOVEM.L    (A7)+,A2
  3224.     RTS
  3225.  
  3226. .REMHEAD
  3227.     MOVE.L    A2,A0
  3228.     BSR.S    _REMHEAD
  3229.     BRA.S    .END
  3230.  
  3231. .REMTAIL
  3232.     MOVE.L    A2,A0
  3233.     BSR.S    _REMTAIL
  3234.     BRA.S    .END
  3235.  
  3236.  
  3237. ; < A0: list
  3238. ; > D0: node
  3239. _REMHEAD
  3240.     MOVE.L    (A0),A1
  3241.     TST.L    (A1)
  3242.     BEQ.S    .EMPTYLIST
  3243.     MOVE.L    A2,-(A7)
  3244.     MOVE.L    (A0),A2
  3245.     MOVE.L    A2,D0
  3246.     MOVE.L    (A2),-(A7)
  3247.     TST.L    (A7)+
  3248.     BEQ.S    .SINGLEELEMENT
  3249.     MOVE.L    LN_PRED(A2),D1
  3250.     MOVE.L    (A2),A2
  3251.     MOVE.L    D1,LN_PRED(A2)
  3252.     MOVE.L    A2,(A0)
  3253.     MOVE.L    (A7)+,A2
  3254.     RTS
  3255.  
  3256. .SINGLEELEMENT
  3257.     MOVE.L    (A0),A2
  3258.     MOVE.L    (A2),(A0)
  3259.     MOVE.L    LN_PRED(A2),LH_TAILPRED(A0)
  3260.     MOVE.L    (A7)+,A2
  3261.     RTS
  3262.  
  3263. .EMPTYLIST
  3264.     MOVEQ.L    #0,D0
  3265.     RTS
  3266.  
  3267. _REMTAIL
  3268.     MOVE.L    (A0),A1
  3269.     TST.L    (A1)
  3270.     BEQ.S    .EMPTYLIST
  3271.     MOVE.L    A2,-(A7)
  3272.     MOVE.L    LH_TAILPRED(A0),A2
  3273.     MOVE.L    (A2),-(A7)
  3274.     TST.L    (A7)+
  3275.     BEQ.S    .SINGLEELEMENT
  3276.     MOVE.L    A2,D0
  3277.     MOVE.L    (A2),D1
  3278.     MOVE.L    LN_PRED(A2),A2
  3279.     MOVE.L    D1,(A2)
  3280.     MOVE.L    A2,LH_TAILPRED(A0)
  3281.     MOVE.L    (A7)+,A2
  3282.     RTS
  3283.  
  3284. .SINGLEELEMENT
  3285.     MOVE.L    A2,D0
  3286.     MOVE.L    (A2),(A0)
  3287.     MOVE.L    LN_PRED(A2),LH_TAILPRED(A0)
  3288.     MOVE.L    (A7)+,A2
  3289.     RTS
  3290.  
  3291. .EMPTYLIST
  3292.     MOVEQ.L    #0,D0
  3293.     RTS
  3294.     ENDIF
  3295.  
  3296. ****************************************************************************
  3297. * INTERRUPTPART IN EXEC                                                    *
  3298. ****************************************************************************
  3299.  
  3300. ; software interrupts (called when Cause() is called)
  3301. ; not List-style because I don't need it right now
  3302. ; Darkseed works OK with this
  3303.  
  3304. INT_64:
  3305.         movem.l    d0-d2/a0-a2/a5-a6,-(a7)
  3306.         move.w    (_custom+intreqr),d0
  3307.         and.w    #INTF_SOFTINT,d0
  3308.         beq    .softint_end
  3309.  
  3310.     ;call handler/server
  3311.  
  3312.         move.l    (4).W,a2
  3313.         lea    (SoftInts,A2),A2    ; soft int
  3314.         moveq.l    #4,D2
  3315. .loop
  3316.         move.l    (A2),D0
  3317.         beq.b    .next
  3318.         move.l    D0,A5
  3319.  
  3320.         move.B    #NT_INTERRUPT,$0008(A5)    ; acknowledge ?
  3321.  
  3322.         pea    .cont(pc)
  3323.         MOVE.L    ($12,A5),-(A7)    ; code
  3324.         move.l    ($E,A5),a1    ; data
  3325.         bsr    InitInterruptRegs
  3326.         RTS                ; call interrupt
  3327. .cont
  3328.  
  3329. .next
  3330.         lea    ($10,A2),A2
  3331.         dbf    D2,.loop
  3332.  
  3333. .softint_end
  3334.         move.w    #INTF_SOFTINT,(_custom+intreq)
  3335.         movem.l    (a7)+,d0-d2/a0-a2/a5-a6
  3336.         rte
  3337.  
  3338. INT_68        movem.l    d0-d1/a0-a1/a5-a6,-(a7)
  3339.         move.w    (_custom+intreqr),d0
  3340.         and.w    #INTF_PORTS,d0
  3341.         beq.b    .ports_end
  3342.  
  3343.     ;call handler/server
  3344. .ports
  3345.         PEA    .ports_cont(PC)
  3346.         move.l    (4).W,a1
  3347.         MOVE.L    (IVPORTS+IV_CODE,A1),-(A7)
  3348.         move.l    (IVPORTS+IV_DATA,a1),a1
  3349.         bsr    InitInterruptRegs
  3350.         RTS
  3351.  
  3352. .ports_cont
  3353. .ports_end
  3354.         move.w    #INTF_PORTS,(_custom+intreq)
  3355.         movem.l    (a7)+,d0-d1/a0-a1/a5-a6
  3356.         rte
  3357.  
  3358. ;---------------
  3359.  
  3360. INT_6c:
  3361.         ; Level 3 interrupt
  3362.  
  3363. ;;;        movem.l    d0-d1/a0-a1/a5-a6,-(a7)
  3364.         movem.l    d0-a6,-(a7)        ; save all registers
  3365.  
  3366.         ; JOTD: I put multitask switching at this level
  3367.  
  3368.         bsr    HandleTaskSwitching
  3369.  
  3370.         ; continue level 3 interrupt
  3371.  
  3372.         move.w    (_custom+intreqr),d0
  3373.         and.w    #INTF_BLIT!INTF_COPER!INTF_VERTB,d0
  3374.         AND.W    (_custom+intenar),d0
  3375.         beq    .vertb_vend        ; no interrupts
  3376.         btst    #INTB_COPER,d0        ; copper interrupt?
  3377.         bne    .vertb_copper
  3378.         btst    #INTB_VERTB,d0        ; vertical blank interrupt?
  3379.         bne.b    .vertb
  3380.  
  3381.     ; it's the blitter
  3382.  
  3383.     PEA    .blt_cont(PC)
  3384.     move.l    (4).W,a1
  3385.     MOVE.L    (IVBLIT+IV_CODE,A1),-(A7)
  3386.     move.l    (IVBLIT+IV_DATA,a1),a1
  3387.     bsr    InitInterruptRegs
  3388.     RTS
  3389. .blt_cont
  3390.     ; acknowledge blitter interrupt
  3391.  
  3392.     move.w    #INTF_BLIT,(_custom+intreq)
  3393.     bra.w    .vertb_vend
  3394.  
  3395.     ; vertical blank
  3396.  
  3397. .vertb
  3398.     bsr    .reload_copper    ; JOTD: moved this to copper interrupt, but
  3399.                 ; this removed the display of Arcade Pool...
  3400.  
  3401.     ;read potinp value
  3402.  
  3403.     bsr    ReadPotInp
  3404.     move.w    D0,potinp_value
  3405.  
  3406.     ;store CIA-B timer value HI+LO
  3407.  
  3408.     bsr    ReadTimerB
  3409.     move.w    D0,VBL_TimerBValue
  3410.  
  3411.     ;timer++
  3412.     
  3413.     move.l    microsec_vbl_interval,D0
  3414.     add.l    D0,microsec_timer_vbl
  3415.     cmp.l    #1000000,microsec_timer_vbl
  3416.     bcs.b    .skip
  3417.  
  3418.     ; one more second in our lives...
  3419.  
  3420.     addq.l    #1,sec_timer
  3421.     clr.l    microsec_timer_vbl
  3422. .skip
  3423.     ; stores microsec_timer_vbl value in another
  3424.     ; variable, so timer.device can update it
  3425.  
  3426.     move.l    microsec_timer_vbl,microsec_timer
  3427.  
  3428.     ;set sprites
  3429.  
  3430.     move.l    _gfxbase(pc),D0
  3431.     beq.b    .ns
  3432.     move.l    D0,A0
  3433.     tst.b    (gb_SpriteReserved,a0)
  3434.     beq    .ns
  3435.     lea    (_sprites),a0
  3436.     lea    (_custom+sprpt),a1
  3437.     moveq    #7,d0
  3438. .ss
  3439.     move.l    (a0)+,d1
  3440.     bmi.b    .skipspr
  3441.     move.l    D1,(a1)
  3442. .skipspr
  3443.     addq.l    #4,A1
  3444.     dbf    d0,.ss
  3445. .ns
  3446.  
  3447.     ;call handler/server
  3448.     PEA    .vertb_cont(PC)
  3449.     move.l    (4).W,a1
  3450.     MOVE.L    (IVVERTB+IV_CODE,A1),-(A7)
  3451.     move.l    (IVVERTB+IV_DATA,a1),a1
  3452.     bsr    InitInterruptRegs
  3453.     RTS
  3454.  
  3455. .vertb_cont
  3456.     ;input handler
  3457.     move.l    _inputhandler(PC),d0
  3458.     beq    .vertb_end
  3459.     moveq    #$6c,d0
  3460.     bsr    _InputHandlerVBI
  3461. .vertb_end
  3462.         ; acknowledge vblank interrupt
  3463.  
  3464.     move.w    #INTF_VERTB,(_custom+intreq)
  3465.  
  3466. .vertb_vend
  3467.     movem.l    (a7)+,d0-a6
  3468.     rte
  3469.  
  3470. ; copper interrupt
  3471.  
  3472. .vertb_copper
  3473.     ;set copperlist (this should be the first !)
  3474.  
  3475.     bsr    .reload_copper
  3476.  
  3477.     ;call copperhandler/server
  3478.     PEA    .vertb_cend(PC)
  3479.     move.l    (4).W,a1
  3480.     MOVE.L    (IVCOPER+IV_CODE,A1),-(A7)
  3481.     move.l    (IVCOPER+IV_DATA,a1),a1
  3482.     bsr    InitInterruptRegs
  3483.     RTS
  3484.  
  3485. .vertb_cend
  3486.         ; acknowledge copper interrupt
  3487.  
  3488.         move.w    #INTF_COPER,(_custom+intreq)
  3489.         bra.s    .vertb_vend
  3490.  
  3491. .reload_copper:
  3492.     move.l    _gfxbase(PC),a0
  3493.     cmp.l    #0,A0
  3494.     beq.b    .skipcop
  3495.     move.l    (gb_LOFlist,a0),D0
  3496.     beq.b    .skipcop
  3497.     move.l    D0,(_custom+cop2lc)
  3498.     move.l    D0,OSM_COPLIST2
  3499. .skipcop
  3500.     rts
  3501.  
  3502.  
  3503. VBL_TimerBValue:
  3504.     dc.w    0
  3505.  
  3506. ; reads RMB/2nd joystick button
  3507. ; only if lowlevel is open or an input handler is installed
  3508.  
  3509. ReadPotInp:
  3510.     tst.l    _lowlbase
  3511.     bne.b    .dotest
  3512.     tst.l    _inputhandler
  3513.     bne.b    .dotest
  3514.     rts
  3515. .dotest
  3516.     move.w    potinp+_custom,D0
  3517.     btst    #14,D0
  3518.     beq.b    .reset
  3519.     btst    #6,D0
  3520.     bne.b    .noreset
  3521. .reset
  3522.     move.w    #$CC01,(potgo+_custom)    ; reset ports
  3523. .noreset
  3524.     rts
  3525.  
  3526. ; < D0: timer value
  3527. ReadTimerB:
  3528.     movem.l    D1/D2,-(A7)
  3529.     sub.w    D1,D1
  3530.     sub.w    D2,D2
  3531.  
  3532.     move.b    $BFE601,D1    ; read LSB
  3533.     move.b    $BFE701,D0    ; read MSB
  3534.     move.b    $BFE601,D2    ; read LSB again
  3535.  
  3536.     cmp.w    D1,D2
  3537.     bcc.b    .cycle        ; D2 passed from $0x to $Fx or something like that
  3538. .exit:
  3539.     lsl.w    #8,D0
  3540.     or.b    D1,D0
  3541.     movem.l    (A7)+,D1/D2
  3542.     rts
  3543. .cycle:
  3544.     subq.b    #1,D0
  3545.     bra.b    .exit
  3546.  
  3547. ;---------------
  3548.  
  3549. INT_70        movem.l    d0-d1/D6/a0-a1/a5-a6,-(a7)
  3550.         move.w    (_custom+intreqr),d6
  3551.         AND.W    (_custom+intenar),d6
  3552.         and.w    #INTF_AUD0!INTF_AUD1!INTF_AUD2!INTF_AUD3,d6
  3553.         BTST    #7,D6
  3554.         BEQ.S    .AUD1
  3555.  
  3556.     ;call handler/server
  3557.         move.l    #$80,d1
  3558.         PEA    .aud0_cont(PC)
  3559.         move.l    (4).W,a1
  3560.         MOVE.L    (IVAUD0+IV_CODE,A1),-(A7)
  3561.         move.l    (IVAUD0+IV_DATA,a1),a1
  3562.         bsr    InitInterruptRegs
  3563.         RTS
  3564.  
  3565. .aud0_cont    move.w    #INTF_AUD0,(_custom+intreq)
  3566.         BCLR    #7,D6
  3567.  
  3568. .AUD1        BTST    #8,D6
  3569.         BEQ.S    .AUD2
  3570.  
  3571.     ;call handler/server
  3572.         move.l    #$100,d1
  3573.         PEA    .aud1_cont(PC)
  3574.         move.l    (4).W,a1
  3575.         MOVE.L    (IVAUD1+IV_CODE,A1),-(A7)
  3576.         move.l    (IVAUD1+IV_DATA,a1),a1
  3577.         bsr    InitInterruptRegs
  3578.         RTS
  3579.  
  3580. .aud1_cont    MOVE.W    #INTF_AUD1,(_custom+intreq)
  3581.         BCLR    #8,D6
  3582.  
  3583. .AUD2        BTST    #9,D6
  3584.         BEQ.S    .AUD3
  3585.  
  3586.     ;call handler/server
  3587.         move.l    #$200,d1
  3588.         PEA    .aud2_cont(PC)
  3589.         move.l    (4).W,a1
  3590.         MOVE.L    (IVAUD2+IV_CODE,A1),-(A7)
  3591.         move.l    (IVAUD2+IV_DATA,a1),a1
  3592.         bsr    InitInterruptRegs
  3593.         RTS
  3594.  
  3595. .aud2_cont    MOVE.W    #INTF_AUD2,(_custom+intreq)
  3596.         BCLR    #9,D6
  3597.  
  3598. .AUD3        BTST    #$A,D6
  3599.         BEQ.S    .aud_end
  3600.  
  3601.     ;call handler/server
  3602.         move.l    #$400,d1
  3603.         PEA    .aud3_cont(PC)
  3604.         move.l    (4).W,a1
  3605.         MOVE.L    (IVAUD3+IV_CODE,A1),-(A7)
  3606.         move.l    (IVAUD3+IV_DATA,a1),a1
  3607.         bsr    InitInterruptRegs
  3608.         RTS
  3609.  
  3610. .aud3_cont    MOVE.W    #INTF_AUD3,(_custom+intreq)
  3611.         BCLR    #$A,D6
  3612.  
  3613. .aud_end    movem.l    (a7)+,d0-d1/D6/a0-a1/a5-a6
  3614.         rte
  3615.  
  3616. INTSERVNODE_AUDIO0
  3617.         dc.l    0        ;LN_SUCC
  3618.         dc.l    0        ;LN_PRED
  3619.         dc.b    NT_INTERRUPT    ;LN_TYPE
  3620.         dc.b    120        ;LN_PRI
  3621.         dc.l    0        ;LN_NAME
  3622.         dc.l    0        ;IS_DATA
  3623.         dc.l    _AUD0_CODE    ;IS_CODE
  3624.  
  3625. INTSERVNODE_AUDIO1
  3626.         dc.l    0        ;LN_SUCC
  3627.         dc.l    0        ;LN_PRED
  3628.         dc.b    NT_INTERRUPT    ;LN_TYPE
  3629.         dc.b    120        ;LN_PRI
  3630.         dc.l    0        ;LN_NAME
  3631.         dc.l    0        ;IS_DATA
  3632.         dc.l    _AUD1_CODE    ;IS_CODE
  3633.  
  3634. INTSERVNODE_AUDIO2
  3635.         dc.l    0        ;LN_SUCC
  3636.         dc.l    0        ;LN_PRED
  3637.         dc.b    NT_INTERRUPT    ;LN_TYPE
  3638.         dc.b    120        ;LN_PRI
  3639.         dc.l    0        ;LN_NAME
  3640.         dc.l    0        ;IS_DATA
  3641.         dc.l    _AUD2_CODE    ;IS_CODE
  3642.  
  3643. INTSERVNODE_AUDIO3
  3644.         dc.l    0        ;LN_SUCC
  3645.         dc.l    0        ;LN_PRED
  3646.         dc.b    NT_INTERRUPT    ;LN_TYPE
  3647.         dc.b    120        ;LN_PRI
  3648.         dc.l    0        ;LN_NAME
  3649.         dc.l    0        ;IS_DATA
  3650.         dc.l    _AUD3_CODE    ;IS_CODE
  3651.  
  3652. _AUD0_CODE
  3653. _AUD1_CODE
  3654. _AUD2_CODE
  3655. _AUD3_CODE
  3656.         RTS
  3657.  
  3658. InitInterruptRegs:
  3659.     lea    _custom,A0            ; added by Jeff
  3660.     move.l    $4.W,A6
  3661.     move.w    (intenar,A0),D0
  3662.     and.w    (intreqr,A0),D0
  3663.     rts
  3664.  
  3665. ;---------------
  3666.  
  3667. INT_78        movem.l    d0-d1/a0-a1/a5-a6,-(a7)
  3668.         move.w    (_custom+intreqr),d0
  3669.         and.w    #INTF_EXTER,d0
  3670.         beq    .exter_end
  3671.  
  3672.     ;call handler/server
  3673. .exter        PEA    .exter_cont(PC)
  3674.         move.l    (4).W,a1
  3675.         MOVE.L    (IVEXTER+IV_CODE,A1),-(A7)
  3676.         move.l    (IVEXTER+IV_DATA,a1),a1
  3677.         RTS
  3678. .exter_cont
  3679. .exter_end
  3680.         move.w    #INTF_EXTER,(_custom+intreq)
  3681.         movem.l    (a7)+,d0-d1/a0-a1/a5-a6
  3682.         rte
  3683.  
  3684.  
  3685. ;---------------
  3686.  
  3687. INT_SERVER
  3688.         move.l    (IV_NODE,a1),-(a7)
  3689. .loop        move.l    (a7),d0
  3690.         beq    .end
  3691.         move.l    d0,a0
  3692.         move.l    (LN_SUCC,a0),(a7)
  3693.         move.l    (IS_DATA,a0),a1
  3694.         TST.L    (IS_CODE,A0)
  3695.         BEQ.S    .loop
  3696.         move.l    (IS_CODE,a0),a6
  3697.         pea    (.loop)
  3698.         lea    (_custom),a0    ;some require this
  3699.         jmp    (a6)
  3700. .end        addq.l    #4,a7
  3701.         rts
  3702.  
  3703.  
  3704. ; Cause, added by JOTD
  3705. ; ripped from ROM and commented (so Harry is happy :))
  3706. ; < A1: Interrupt
  3707.  
  3708.  
  3709. _Cause:
  3710.     DISABLE
  3711.  
  3712.     MOVEQ    #NT_SOFTINT,D0
  3713.     CMP.B    $0008(A1),D0
  3714.     BEQ.B    .LB_181A        ; already softint, go out
  3715.     MOVE.B    D0,LN_TYPE(A1)        ; sets to soft int type
  3716.     MOVE.B    LN_PRI(A1),D0
  3717.     AND.W    #$00F0,D0        ; masks cos' only -32,-16,0,16,32 are supported
  3718.     EXT.W    D0
  3719.     LEA    $01D2(A6),A0        ; SOFTINT2
  3720.     ADDA.W    D0,A0            ; -32: offset 0, -16: offset 1 and so on...
  3721.     MOVE.L    A1,(A0)            ; not exact but who cares ATM...
  3722.  
  3723.     MOVE.W    #$8004,$00DFF09C    ; interrupt requested
  3724.     BSET    #$05,$0124(A6)
  3725. .LB_181A
  3726.  
  3727.     SUBQ.B    #1,$0126(A6)        ; enable
  3728.     BGE.B    .LB_1828
  3729.     MOVE.W    #$C004,$00DFF09A    ; + allow level 1 interrupt
  3730. .LB_1828
  3731.     NOP    
  3732.     RTS    
  3733.  
  3734.  
  3735.     IFD    XXXXXXXXX
  3736. _RealCause:
  3737.     DISABLE
  3738.  
  3739.     MOVEQ    #$0B,D0
  3740.     CMP.B    $0008(A1),D0
  3741.     BEQ.B    .LB_181A        ; already softint, go out
  3742.     MOVE.B    D0,LN_TYPE(A1)        ; sets to soft int type
  3743.     MOVE.B    LN_PRI(A1),D0
  3744.     AND.W    #$00F0,D0        ; masks cos' only -32,-16,0,16,32 are supported
  3745.     EXT.W    D0
  3746.     LEA    $01D2(A6),A0        ; SOFTINT2
  3747.     ADDA.W    D0,A0            ; -32: offset 0, -16: offset 1 and so on...
  3748.     ADDQ.L    #4,A0            ; tail of list
  3749.     MOVE.L    $0004(A0),D0        ; tailpred
  3750.     MOVE.L    A1,$0004(A0)        ; sets current interrupt in tailpred
  3751.     EXG    D0,A0
  3752.     MOVEM.L    D0/A0,(A1)
  3753.     MOVE.L    A1,(A0)            ; ends linking interrupt in the softint list
  3754.     MOVE.W    #$8004,$00DFF09C    ; interrupt requested
  3755.     BSET    #$05,$0124(A6)
  3756. .LB_181A
  3757.  
  3758.     ENABLE
  3759.     NOP    
  3760.     RTS    
  3761.     ENDC
  3762.  
  3763.  
  3764. ; set int vector, replaced old Harry code by direct 3.1 ROM rip (JOTD)
  3765. ; with a test against $DDDDDDDD value
  3766. ; (original ROM sets to 0 the default interrupt memory I think, but OSEmu
  3767. ; prefers to trash this zone to avoid bugs)
  3768.  
  3769. _SETINTVECTOR:    ;D0-INT#, A1-INTERRUPTNODE, RET D0-OLD INTERRUPTNODE
  3770.     tst.l    D0
  3771.     beq.b    .tbe_ok
  3772.     CMP.L    #$4,D0
  3773.     BLO.S    .NOTSUPPORTED
  3774.     CMP.L    #$C,D0
  3775.     BHS.S    .NOTSUPPORTED
  3776.  
  3777.  
  3778. .tbe_ok
  3779.     MULU.W    #$000C,D0
  3780.     LEA    $54(A6,D0.W),A0
  3781.     DISABLE
  3782.     MOVE.L    $0008(A0),D0
  3783.     MOVE.L    A1,$0008(A0)
  3784.     BEQ.B    .LB_16AA
  3785.  
  3786.     cmp.l    #VAL_DDDDDDDD,A1    ; added
  3787.     BEQ.B    .LB_16AA        ; added
  3788.  
  3789.     MOVE.L    $000E(A1),(A0)
  3790.     MOVE.L    $0012(A1),$0004(A0)
  3791.     BRA.B    .LB_16B2
  3792. .LB_16AA
  3793.     MOVEQ    #-$01,D1
  3794.     MOVE.L    D1,(A0)
  3795.     MOVE.L    D1,$0004(A0)
  3796. .LB_16B2
  3797.     ENABLE
  3798. .ignore
  3799.     RTS    
  3800.  
  3801. .NOTSUPPORTED
  3802.     pea    _LVOSetIntVector
  3803.     pea    _execname
  3804.     bra    _emufail
  3805.  
  3806.  
  3807.  
  3808. ;d0=intnumber a1=interrupt
  3809.  
  3810. _AddIntServer:
  3811.         movem.l    d6/a2-a4,-(a7)
  3812.         move.l    d0,d6            ; save interrupt number for later use
  3813.         mulu    #IV_SIZE,D0
  3814.         lea    (IntVects+IV_NODE,a6,d0.l),a2    ;a2 = list base
  3815.         move.w    #INTF_INTEN,(_custom+intena)
  3816.  
  3817.         move.b    (LN_PRI,a1),d1            ;d1 = priority
  3818.         sub.l    a3,a3                ;a3 = predecessor
  3819.         move.l    (a2),a4                ;a4 = successor
  3820.  
  3821. .next        move.l    a4,d0
  3822.         beq    .hangin
  3823.         cmp.b    (LN_PRI,a4),d1
  3824.         bgt    .hangin
  3825.         move.l    a4,a3
  3826.         move.l    (LN_SUCC,a3),a4
  3827.         bra    .next
  3828.  
  3829. .hangin        move.l    a3,(LN_PRED,a1)
  3830.         bne    .hi1
  3831.         move.l    a2,a3                ;LN_SUCC == 0 !...
  3832. .hi1        move.l    a1,(LN_SUCC,a3)
  3833.         move.l    a4,(LN_SUCC,a1)
  3834.         beq    .hi2
  3835.         move.l    a1,(LN_PRED,a4)
  3836. .hi2
  3837. .end        move.w    #INTF_SETCLR!INTF_INTEN,d0    ; JOTD: added specific interrupt
  3838.         bset    d6,d0
  3839.         move.w    d0,(_custom+intena)    ; enable for DreamWeb. See RKRM!
  3840.         movem.l    (a7)+,a2-a4/d6
  3841.         rts
  3842.  
  3843.  ifeq 1
  3844.     MOVE.L    D2,-(A7)
  3845.     MOVE.L    D0,D2
  3846.     MOVE.L    D0,D1
  3847.     MULU.W    #IV_SIZE,D0
  3848.     LEA       (IntVects,A6,D0.W),A0
  3849.     MOVE.L    (A0),A0
  3850.     MOVE.W    #INTF_INTEN,(_custom+intena)
  3851.     ADDQ.B    #1,(IDNestCnt,A6)
  3852.     BSR.W     _Enqueue
  3853.     MOVE.W    #-$8000,D0
  3854.     BSET      D2,D0
  3855.     MOVE.W    D0,(_custom+intena)
  3856.     SUBQ.B    #1,(IDNestCnt,A6)
  3857.     BGE.S     .1654
  3858.     MOVE.W    #INTF_SETCLR!INTF_INTEN,(_custom+intena)
  3859. .1654    MOVE.L    (A7)+,D2
  3860.     RTS
  3861.  endc
  3862.  
  3863. _RemIntServer    ;d0=intnumber a1=interrupt
  3864.  
  3865.     ;this is a workaround for Millenium2·2, which does not set D0 correctly
  3866.         cmp.l    #$20,d0
  3867.         blo    .ok
  3868.         moveq    #5,d0
  3869. .ok
  3870.         movem.l    a2-a4,-(a7)
  3871.         mulu    #IV_SIZE,D0
  3872.         lea    (IntVects+IV_NODE,a6,d0.l),a2    ;a2 = list base
  3873.         move.w    #INTF_INTEN,(_custom+intena)
  3874.  
  3875.         sub.l    a3,a3                ;a3 = predecessor
  3876.         move.l    a2,a0
  3877.         
  3878. .next        move.l    (LN_SUCC,a0),a0
  3879.         cmp.l    a0,a1
  3880.         beq    .hangout
  3881.         move.l    a0,a3
  3882.         bra    .next
  3883.  
  3884. .hangout    move.l    (LN_SUCC,a1),a4            ;a4 = successor
  3885.         move.l    a4,d0
  3886.         beq    .ho1
  3887.         move.l    a3,(LN_PRED,a4)
  3888. .ho1        move.l    a3,d0
  3889.         bne    .ho2
  3890.         move.l    a2,a3                ;LN_SUCC == 0 !...
  3891. .ho2        move.l    a4,(LN_SUCC,a3)
  3892.  
  3893. .end        move.w    #INTF_SETCLR!INTF_INTEN,(_custom+intena)
  3894.         movem.l    (a7)+,a2-a4
  3895.         rts
  3896.  
  3897. _ExitIntr    MOVEM.L    (A7)+,D0-D1/A0-A1/A5-A6
  3898.         RTE
  3899.  
  3900. **************************************************************************
  3901. *   LIBRARY FUNCTIONS                                                    *
  3902. **************************************************************************
  3903.  
  3904. OPENLIB:
  3905.         move.l    a2,-(a7)
  3906.  
  3907.         lea    _libtable,a2
  3908. .next        move.l    (a2)+,a0
  3909.         move.l    a0,d0
  3910.         beq    .err
  3911.         bsr    _strcmp
  3912.         beq    .found
  3913.         addq.l    #4,a2
  3914.         bra    .next
  3915.  
  3916. .found        move.l    (a2),a0
  3917.         jsr    (a0)        ;init
  3918.         
  3919.         move.l    (a7)+,a2
  3920.         rts
  3921.  
  3922. .err
  3923.     EMUFAIL    _LVOOpenLibrary,_execname
  3924.  
  3925. **************************************************************************
  3926. *   DEVICE FUNCTIONS                                                     *
  3927. **************************************************************************
  3928.  
  3929. ;a0=name d0=unit a1=ioreq d1=flags
  3930.  
  3931. _OpenDevice:
  3932.         moveM.l    D0/A0-a2,-(a7)
  3933.  
  3934.         MOVE.L    A0,A1
  3935.         lea    _devtable,a2
  3936. .next        move.l    (a2)+,a0
  3937.         move.l    a0,d0
  3938.         beq    .err
  3939.         bsr    _strcmp
  3940.         beq    .found
  3941.         addq.l    #4,a2
  3942.         bra    .next
  3943.  
  3944. .err
  3945.     EMUFAIL    _LVOOpenDevice,_execname
  3946.  
  3947. .found
  3948.  
  3949.         MOVE.L    8(A7),A1
  3950.     IFNE    CORRECTDEVICES
  3951.         MOVE.L    (A2),(IO_DEVICE,A1)
  3952.     ELSE
  3953.         move.l    A2,-(A7)
  3954.         lea    .namebuf(pc),A2
  3955.         move.b    (A0)+,(A2)
  3956.         move.b    (A0)+,(1,A2)
  3957.         move.b    (A0)+,(2,A2)
  3958.         move.b    (A0)+,(3,A2)
  3959.         move.l    A2,A0        ; now the name in A0 is word aligned
  3960.         move.l    (A7)+,A2
  3961.         move.l    (a0),(IO_DEVICE,a1)
  3962.     ENDC
  3963.         move.l    (A7),(IO_UNIT,a1)
  3964.         move.b    d1,(IO_FLAGS,A1)
  3965.         MOVEM.L    (A7)+,D0/A0-A2
  3966.         moveq    #0,d0
  3967.         move.b    d0,(IO_ERROR,a1)
  3968.         rts
  3969.  
  3970. .namebuf
  3971.     dc.l    0
  3972.  
  3973. _InputHandlerVBI
  3974.     ;check LMB
  3975.         btst    #CIAB_GAMEPORT0,(_ciaa+ciapra)    ; mouse left button?
  3976.         seq    d0
  3977.         cmp.b    (_last_lmb),d0
  3978.         beq    .lmbend            ; same state as before
  3979.         move.b    d0,(_last_lmb)
  3980.         lea    _inputevent,a0
  3981.         clr.l    (ie_NextEvent,a0)
  3982.         move.b    #IECLASS_RAWMOUSE,(ie_Class,a0)
  3983.         clr.b    (ie_SubClass,a0)
  3984.         move.w    #IECODE_LBUTTON,(ie_Code,a0)
  3985.         tst.b    d0
  3986.         bne    .lmb1
  3987.         or.w    #IECODE_UP_PREFIX,(ie_Code,a0)
  3988. .lmb1        moveq    #0,d0
  3989.         move.b    (_last_lmb),d1
  3990.         beq    .lmb2
  3991.         or.w    #IEQUALIFIER_LEFTBUTTON,d0
  3992. .lmb2        move.b    (_last_rmb),d1
  3993.         beq    .lmb3
  3994.         or.w    #IEQUALIFIER_RBUTTON,d0
  3995. .lmb3        move.w    d0,(ie_Qualifier,a0)
  3996.         pea    .lmbend(pc)
  3997.         move.l    _inputhandler,a1
  3998.         move.l    (IS_CODE,a1),-(a7)
  3999.         move.l    (IS_DATA,a1),a1
  4000.         rts                    ;a0=InputEvent a1=HandlerData
  4001. .lmbend
  4002.     ;check RMB
  4003. ;        btst    #POTGOB_DATLY-8,(_custom+potinp)
  4004.         btst    #POTGOB_DATLY-8,potinp_value
  4005.         seq    d0
  4006.         cmp.b    (_last_rmb),d0
  4007.         beq    .rmbend
  4008.         move.b    d0,(_last_rmb)
  4009.         lea    _inputevent,a0
  4010.         clr.l    (ie_NextEvent,a0)
  4011.         move.b    #IECLASS_RAWMOUSE,(ie_Class,a0)
  4012.         clr.b    (ie_SubClass,a0)
  4013.         move.w    #IECODE_RBUTTON,(ie_Code,a0)
  4014.         tst.b    d0
  4015.         bne    .rmb1
  4016.         or.w    #IECODE_UP_PREFIX,(ie_Code,a0)
  4017. .rmb1        moveq    #0,d0
  4018.         move.b    (_last_lmb),d1
  4019.         beq    .rmb2
  4020.         or.w    #IEQUALIFIER_LEFTBUTTON,d0
  4021. .rmb2        move.b    (_last_rmb),d1
  4022.         beq    .rmb3
  4023.         or.w    #IEQUALIFIER_RBUTTON,d0
  4024. .rmb3        move.w    d0,(ie_Qualifier,a0)
  4025.         pea    .rmbend
  4026.         move.l    _inputhandler,a1
  4027.         move.l    (IS_CODE,a1),-(a7)
  4028.         move.l    (IS_DATA,a1),a1
  4029.         rts                    ;a0=InputEvent a1=HandlerData
  4030. .rmbend
  4031.     ;check MouseMove
  4032.         move.w    (_custom+joy0dat),d0
  4033.         move.w    (_last_joy0dat),d1
  4034.         cmp.w    d0,d1
  4035.         beq    .rts                ; no move
  4036.         move.w    d0,(_last_joy0dat)        ; save joy0dat
  4037.         lea    _inputevent,a0
  4038.         clr.l    (ie_NextEvent,a0)
  4039.         move.b    #IECLASS_RAWMOUSE,(ie_Class,a0)
  4040.         clr.b    (ie_SubClass,a0)
  4041.         move.w    #IECODE_NOBUTTON,(ie_Code,a0)
  4042.         clr.w    (ie_Qualifier,a0)
  4043.         movem.l    d2-d3,-(a7)
  4044.         moveq    #0,d2
  4045.         move.b    d1,d2
  4046.         moveq    #0,d3
  4047.         move.b    d0,d3
  4048.         sub.w    d2,d3        ; newdat-olddat
  4049.         cmp.w    #127,d3
  4050.         bgt    .xsub
  4051.         cmp.w    #-128,d3
  4052.         bge    .xok
  4053.         add.w    #512,d3
  4054. .xsub        sub.w    #256,d3
  4055. .xok        move.w    d3,(ie_X,a0)
  4056.         movem.l    (a7)+,d2-d3
  4057.         lsr.w    #8,d0
  4058.         lsr.w    #8,d1
  4059.         sub.w    d1,d0
  4060.         cmp.w    #127,d0
  4061.         bgt    .ysub
  4062.         cmp.w    #-128,d0
  4063.         bge    .yok
  4064.         add.w    #512,d0
  4065. .ysub        sub.w    #256,d0
  4066. .yok        move.w    d0,(ie_Y,a0)
  4067.         move.l    _inputhandler,a1
  4068.         move.l    (IS_CODE,a1),-(a7)
  4069.         move.l    (IS_DATA,a1),a1
  4070. .rts        rts                    ;a0=InputEvent a1=HandlerData
  4071.  
  4072. _InputHandlerPI:
  4073.         lea    _inputevent,a0
  4074.         clr.l    (ie_NextEvent,a0)        ; only one event at a time!
  4075.         move.b    #IECLASS_RAWKEY,(ie_Class,a0)
  4076.         clr.b    (ie_SubClass,a0)
  4077.         moveq    #0,d0
  4078.         move.b    (KBDQUALIFIER+1),d0
  4079.         move.w    d0,(ie_Code,a0)
  4080.  
  4081.         moveq    #0,d0
  4082.         move.b    (KBDQUALIFIER),d0
  4083.         move.w    D0,(ie_Qualifier,a0)
  4084.         move.l    _inputhandler,a1
  4085.         move.l    (IS_CODE,a1),-(a7)
  4086.         move.l    (IS_DATA,a1),a1
  4087.         rts                    ;a0=InputEvent a1=HandlerData
  4088.  
  4089. _SendIO
  4090. _DoIO        ;a1=ioreq
  4091.         tst.l    (IO_DEVICE,A1)
  4092.         beq    .cd                ; not found: ignore! (JOTD)
  4093.  
  4094.         move.w    (IO_COMMAND,a1),d0        ;d0=command
  4095.     IFNE    CORRECTDEVICES
  4096.         MOVE.L    _tddevtable+4(PC),D1
  4097.         CMP.L    (IO_DEVICE,A1),D1
  4098.     ELSE
  4099.         cmp.l    #"trac",(IO_DEVICE,a1)
  4100.     ENDC
  4101.         beq    .trackdisk
  4102.     IFNE    CORRECTDEVICES
  4103.         MOVE.L    _inpdevtable+4(PC),D1
  4104.         CMP.L    (IO_DEVICE,A1),D1
  4105.     ELSE
  4106.         cmp.l    #"inpu",(IO_DEVICE,a1)
  4107.     ENDC
  4108.         beq    .input
  4109.  
  4110.     ; cd.device added by Jeff
  4111.  
  4112.     IFNE    CORRECTDEVICES
  4113.         MOVE.L    _cddevtable+4(PC),D1
  4114.         CMP.L    (IO_DEVICE,A1),D1
  4115.     ELSE
  4116.         cmp.l    #"cd.d",(IO_DEVICE,a1)
  4117.     ENDC
  4118.         beq    .cd
  4119.  
  4120.     ; cdtv.device added by Jeff
  4121.  
  4122.     IFNE    CORRECTDEVICES
  4123.         MOVE.L    _cdtvdevtable+4(PC),D1
  4124.         CMP.L    (IO_DEVICE,A1),D1
  4125.     ELSE
  4126.         cmp.l    #"cdtv",(IO_DEVICE,a1)
  4127.     ENDC
  4128.         beq    .cd
  4129.  
  4130.     ; keyboard.device added by Jeff
  4131.  
  4132.     IFNE    CORRECTDEVICES
  4133.         MOVE.L    _kbdevtable+4(PC),D1
  4134.         CMP.L    (IO_DEVICE,A1),D1
  4135.     ELSE
  4136.         cmp.l    #"keyb",(IO_DEVICE,a1)
  4137.     ENDC
  4138.         beq    .keyboard
  4139.  
  4140.  
  4141.     ; timer.device added by Jeff
  4142.  
  4143.     IFNE    CORRECTDEVICES
  4144.         MOVE.L    _timerdevtable+4(PC),D1
  4145.         CMP.L    (IO_DEVICE,A1),D1
  4146.     ELSE
  4147.         cmp.l    #"time",(IO_DEVICE,a1)
  4148.     ENDC
  4149.     beq    .timer
  4150.  
  4151. .fail
  4152.     move.l    D0,D6
  4153.  
  4154.     EMUFAIL    _LVODoIO,_execname
  4155.         
  4156. .cd
  4157.         moveq.l    #0,D0    ; cd.device,cdtv.device: do nothing
  4158.         rts
  4159.  
  4160. ; DoIO on timer.device, added by JOTD
  4161.  
  4162. .timer
  4163.     move.l    ($18,A1),D0
  4164.     cmp.l    #UNIT_MICROHZ,D0
  4165.     beq    .ok
  4166.     cmp.l    #UNIT_VBLANK,D0
  4167.     beq    .ok
  4168.     
  4169.     ; UNIT_ECLOCK,UNIT_WAITUNTIL,UNIT_WAITECLOCK not supported yet
  4170.  
  4171.     bra.b    .fail        ; timer device not supported yet, too bad
  4172.     rts
  4173.  
  4174. ; can be VBLANK or MICROHZ
  4175. .ok
  4176.     move.w    (IO_COMMAND,A1),D1
  4177.     cmp.w    #TR_GETSYSTIME,D1
  4178.     bne.b    .fail
  4179.  
  4180.     ; system time requested
  4181.     ; (valable for PAL/NTSC)
  4182.  
  4183.     move.l    sec_timer,D1
  4184.     move.l    D1,(TV_SECS+IO_SIZE,A1)    ; seconds computed OK, now microsecs...
  4185.  
  4186.     move.l    microsec_timer,D1
  4187.  
  4188.     cmp.l    #UNIT_MICROHZ,($18,A1)
  4189.     bne.b    .endcia
  4190.  
  4191.     ; UNIT_MICROHZ, we need to be more accurate
  4192.     
  4193.     bsr    ReadTimerB
  4194.     move.l    D2,-(A7)
  4195.     move.w    D0,D2        ; new value
  4196.     sub.w    VBL_TimerBValue,D0
  4197.     move.w    D2,VBL_TimerBValue    ; update value
  4198.     move.l    (a7)+,D2
  4199.     tst.w    D0
  4200.     bpl.b    .positive
  4201.     neg.w    D0        ; take absolute value
  4202. .positive
  4203.  
  4204.     ; we need to *1.4... to compute the real number of microsecs
  4205.     ; we do better: multiplicate by approx (1000000*0x100)/0xAD303
  4206.     ; and then shift right by 16 bits
  4207.  
  4208.     mulu    #361,D0
  4209.     lsr.l    #8,D0
  4210.     add.l    D0,D1    ; adds to microsecs computed from VBL interrupt
  4211.  
  4212.     move.l    D1,microsec_timer    ; update microsec timer for better accurracy
  4213.  
  4214. .endcia
  4215.  
  4216.     move.l    D1,(TV_MICRO+IO_SIZE,A1)
  4217.     moveq.l    #0,D0
  4218.     clr.b    (IO_ERROR,A1)
  4219.     rts
  4220.  
  4221. .keyboard
  4222.         cmp.w    #KBD_READMATRIX,d0
  4223.         beq    .kb_readmatrix
  4224.         cmp.w    #KBD_READEVENT,d0
  4225.         beq    .kb_readevent
  4226.  
  4227.  
  4228.         cmp.w    #CMD_CLEAR,d0
  4229.  
  4230.         BRA    .fail
  4231.  
  4232. ; read event (Black Tiger)
  4233.  
  4234. .kb_readevent
  4235.     movem.l    A1,-(A7)
  4236.     lea    _inputevent(pc),A0
  4237.     move.l    (IO_DATA,a1),a1
  4238.     move.w    #$15,D0
  4239. .kbcopy
  4240.     move.b    (A0)+,(A1)+
  4241.     dbf    D0,.kbcopy
  4242.     movem.l    (A7)+,A1
  4243.     bra    .kbend
  4244.  
  4245. .kb_readmatrix
  4246.         move.l    a1,-(SP)
  4247.         moveq    #15,d0
  4248.         move.l    (IO_LENGTH,a1),d1
  4249.         move.l    (IO_DATA,a1),a0
  4250.         cmp.l    d0,d1
  4251.         bls.s    .lower
  4252.         move.w    d0,d1
  4253. .lower
  4254.         move.l    d1,(IO_ACTUAL,a1)
  4255.         move.l    (IO_DEVICE,a1),a1
  4256.         lea    $136(a1),a1
  4257.         bra.s    .goto
  4258. .loop
  4259.         move.b    (a1)+,(a0)+
  4260. .goto
  4261.         dbra    d1,.loop
  4262.         movea.l    (SP)+,a1
  4263. .kbend
  4264.         clr.b    (IO_ERROR,a1)
  4265.         moveq.l    #0,d0
  4266.         rts
  4267.  
  4268. .input        cmp.w    #CMD_RESET,d0
  4269.         beq    .ret
  4270.         cmp.w    #IND_ADDHANDLER,d0
  4271.         beq    .i_addh
  4272.         cmp.w    #IND_REMHANDLER,d0
  4273.         beq    .i_remh
  4274.         cmp.w    #IND_SETMPORT,d0
  4275.         beq    .ret
  4276.         cmp.w    #IND_SETMTRIG,d0
  4277.         beq    .i_setmtrig
  4278.         BRA    .fail
  4279.  
  4280. ; adds an input handler
  4281. .i_addh        move.w    (_custom+joy0dat),(_last_joy0dat)
  4282.  
  4283.         move.l    A0,-(A7)
  4284.  
  4285.         tst.l    _inputhandler
  4286.         BEQ    .add
  4287.  
  4288.         ; already an handler installed!
  4289.  
  4290.         move.l    _inputhandler,A0
  4291.  
  4292.         ; check if intuition handler is installed
  4293.  
  4294.         cmp.l    #IntuitionInputHandler,(IS_CODE,A0)
  4295.         bne.b    .fail
  4296.  
  4297.         move.l    (IO_DATA,a1),_inputhandler    ; sets handler
  4298.         bsr    rem_input_handler        ; removes intuition input handler
  4299. .add
  4300.         move.l    (IO_DATA,a1),_inputhandler
  4301.         move.l    (A7)+,A0
  4302.         moveq.l    #0,D0            ; added by JOTD
  4303.         rts
  4304.  
  4305. .i_remh        clr.l    _inputhandler
  4306.         rts
  4307. .i_setmtrig    move.l    (IO_DATA,a1),.gpt
  4308.         moveq.l    #0,D0            ; added by JOTD
  4309.         rts
  4310. .gpt        dc.l    0
  4311.  
  4312. .trackdisk
  4313.         cmp.w    #CMD_READ,d0
  4314.         beq    .td_read
  4315.         cmp.w    #CMD_CLEAR,d0
  4316.         beq    .ret
  4317.         cmp.w    #ETD_READ,d0
  4318.         beq    .td_read
  4319.         cmp.w    #TD_MOTOR,d0
  4320.         beq    .ret
  4321.         cmp.w    #TD_FORMAT,d0
  4322.         beq    .td_write
  4323.         cmp.w    #TD_REMOVE,d0
  4324.         beq    .ret
  4325.         cmp.w    #TD_CHANGENUM,d0
  4326.         beq    .ret
  4327.         cmp.w    #TD_CHANGESTATE,d0
  4328.         beq    .ret
  4329.         cmp.w    #ETD_SEEK,d0
  4330.         beq    .td_seek    ; added by JOTD
  4331.         cmp.w    #TD_SEEK,d0
  4332.         beq    .td_seek    ; added by JOTD
  4333.         cmp.w    #ETD_WRITE,d0
  4334.         beq    .td_write
  4335.         cmp.w    #CMD_WRITE,d0
  4336.         beq    .td_write    ; added by JOTD
  4337.         cmp.w    #ETD_UPDATE,d0
  4338.         beq    .ret
  4339.         cmp.w    #ETD_CLEAR,d0
  4340.         beq    .ret
  4341.         cmp.w    #ETD_MOTOR,d0
  4342.         beq    .ret
  4343.         cmp.w    #CMD_UPDATE,d0    ; added by JOTD
  4344.         beq    .ret
  4345.         BRA.W    .fail
  4346.  
  4347. .td_seek:    ; seek only moves the heads, nothing is read!
  4348.         beq    .ret
  4349.  
  4350.     ;"deuteros" expects that a1 is unchanged !
  4351. .td_read    movem.l    d2/a1,-(a7)
  4352.         move.l    (IO_OFFSET,a1),d0
  4353.         move.l    (IO_LENGTH,a1),d1
  4354.         move.l    d1,(IO_ACTUAL,a1)    ; Mr.Larmer
  4355.         move.l    (IO_UNIT,a1),d2
  4356.         addq.l    #1,d2
  4357.         move.l    (IO_DATA,a1),a0
  4358.         move.l    (_RESLOAD),a1
  4359.         jsr    (resload_DiskLoad,a1)
  4360.         movem.l    (a7)+,d2/a1
  4361. .ret        clr.b    (IO_ERROR,a1)
  4362.         moveq.l    #0,D0            ; added by JOTD
  4363.         rts
  4364.  
  4365. .td_write    tst.l    _p3    ; JOTD: what's this ??? WHDLoad registered protection?
  4366.         bne.b    .td_dowrite
  4367. .td_dowrite
  4368.         movem.l    a1-a2,-(a7)
  4369.         clr.b    (IO_ERROR,a1)
  4370.         move.l    (IO_LENGTH,a1),d0    ;size
  4371.         lea    (.disk),a0
  4372.         move.l    (IO_UNIT,a1),d1
  4373.         add.b    #"1",d1
  4374.         move.b    d1,(5,a0)        ;name
  4375.         move.l    (IO_OFFSET,a1),d1    ;offset
  4376.         move.l    (IO_DATA,a1),a1        ;address
  4377.         move.l    (_RESLOAD),a2
  4378.         jsr    (resload_SaveFileOffset,a2)
  4379.         movem.l    (a7)+,a1-a2
  4380.         rts
  4381. .f        st    (IO_ERROR,a1)
  4382.         rts
  4383.  
  4384. .disk        dc.b    "Disk.",0,0,0
  4385.  
  4386. _SENDAUDIO
  4387.         CLR.B    IO_ERROR(A1)
  4388.         MOVEQ.L    #0,D0
  4389.         RTS
  4390.  
  4391. _WAITIO
  4392.     IFNE    CORRECTDEVICES
  4393.         MOVE.L    _cddevtable+4(PC),D1
  4394.         CMP.L    (IO_DEVICE,A1),D1
  4395.     ELSE
  4396.         cmp.l    #"cd.d",(IO_DEVICE,a1)
  4397.     ENDC
  4398.         beq    .cd
  4399.  
  4400.     IFNE    CORRECTDEVICES
  4401.         MOVE.L    _cdtvdevtable+4(PC),D1
  4402.         CMP.L    (IO_DEVICE,A1),D1
  4403.     ELSE
  4404.         cmp.l    #"cdtv",(IO_DEVICE,a1)
  4405.     ENDC
  4406.         beq    .cd
  4407.  
  4408. ;;.fail
  4409. ;;    EMUFAIL    _LVOWaitIO,_execname
  4410.         
  4411. .cd
  4412.         CLR.B    IO_ERROR(A1)
  4413.         moveq.l    #0,D0    ; cd.device,cdtv.device: do nothing
  4414.         RTS
  4415.  
  4416. _ABORTIO
  4417.         CLR.B    IO_ERROR(A1)
  4418.         MOVEQ.L    #0,D0
  4419.         RTS
  4420.  
  4421. _CHECKIO
  4422.         MOVE.L    A1,D0    ; request has completed
  4423.         RTS
  4424.  
  4425.  
  4426. INTSERVNODE_SP    dc.l    0        ;LN_SUCC
  4427.         dc.l    0        ;LN_PRED
  4428.         dc.b    NT_INTERRUPT    ;LN_TYPE
  4429.         dc.b    0        ;LN_PRI
  4430.         dc.l    0        ;LN_NAME
  4431.         dc.l    DATA_KBD    ;IS_DATA
  4432.         dc.l    INT_KBD        ;IS_CODE
  4433.  
  4434. DATA_KBD    ds.b    $1C0
  4435.  
  4436. INT_KBD
  4437.         movem.l    D2-D4/A6,-(SP)
  4438.         movea.l    A1,A6
  4439.         moveq    #0,D2
  4440.         move.b    $BFEC01,D2
  4441.  
  4442.         MOVE.B    #$41,$BFEE01
  4443.  
  4444. ;        ori.b    #$40,$BFEE01
  4445.  
  4446.         ; acknowledge keyboard
  4447.  
  4448.         MOVEQ.L    #2,D3
  4449. .4        MOVE.L    D1,-(A7)
  4450.         MOVE.B    $DFF006,D1
  4451. .3        CMP.B    $DFF006,D1
  4452.         BEQ.S    .3
  4453.         MOVE.L    (A7)+,D1
  4454.         DBF    D3,.4
  4455.  
  4456.         moveq    #0,D4
  4457.         not.b    D2
  4458.         ror.b    #1,D2
  4459.  
  4460.         ; keyboard is read here
  4461.  
  4462.         MOVE.B    D2,KBDQUALIFIER+1
  4463.  
  4464.  
  4465.         cmpi.b    #$78,D2
  4466.         bne.s    lbC000078
  4467.  
  4468.         illegal        ; should not happen
  4469.  
  4470.     ifeq    1
  4471.         bset    #0,$134(A6)
  4472.         beq.s    lbC00007E
  4473.         bset    #1,$134(A6)
  4474.         bne.w    lbC00012E
  4475.         movea.l    $124(A6),A1
  4476.         tst.l    (A1)
  4477.         beq.w    lbC00012C
  4478. lbC00005E
  4479.         move.l    (A1),D3
  4480.         beq.w    lbC00012E
  4481.         addq.w    #1,$132(A6)
  4482.         move.l    A6,-(SP)
  4483.         movea.l    $24(A6),A6
  4484.         jsr    -$B4(A6)
  4485.         movea.l    (SP)+,A6
  4486.         movea.l    D3,A1
  4487.         bra.s    lbC00005E
  4488.     endc
  4489.  
  4490.  
  4491. lbC000078
  4492.  
  4493.         bclr    #0,$134(A6)
  4494. lbC00007E
  4495.         move.w    D2,D1
  4496.         andi.w    #$7F,D1
  4497.  
  4498.         ; Jff: read shift/ctrl/alt keys
  4499.  
  4500.         bsr    _SetQualifierFlags
  4501.  
  4502.         cmpi.w    #$71,D1
  4503.         bhi.w    lbC0000EA
  4504.  
  4505.         move.w    D1,D0
  4506.         andi.w    #7,D0
  4507.         lsr.w    #3,D1
  4508.         lea    $136(A6),A0
  4509.         btst    #7,D2
  4510.         bne.s    lbC0000AE
  4511.         bset    D0,0(A0,D1.W)
  4512.         cmpi.w    #12,D1
  4513.         bne.s    lbC0000CE
  4514.         bset    D0,$135(A6)
  4515.         bra.s    lbC0000CE
  4516. lbC0000AE
  4517.         bclr    D0,0(A0,D1.W)
  4518.         cmpi.w    #12,D1
  4519.         bne.s    lbC0000CE
  4520.         bclr    D0,$135(A6)
  4521.         bra.s    lbC0000CE
  4522. lbW0000BE
  4523.         dc.w    $80,$E0,$E0,$F0,$804,$7C,0,0
  4524. lbC0000CE
  4525.         move.w    D2,D1
  4526.         andi.w    #$7F,D1
  4527.         move.w    D1,D0
  4528.         andi.w    #7,D0
  4529.         lsr.w    #3,D1
  4530.         lea    lbW0000BE(PC),A0
  4531.         btst    D0,0(A0,D1.W)
  4532.         beq.s    lbC0000EA
  4533.         ori.w    #$100,D4
  4534. lbC0000EA
  4535.         move.w    $72(A6),D1
  4536.         move.w    D1,D0
  4537.         addq.w    #4,D0
  4538.         andi.w    #$7F,D0
  4539.         cmp.w    $70(A6),D0
  4540.         bne.s    lbC000108
  4541.         move.w    D1,D0
  4542.         move.w    #$7D,D2
  4543.         subq.w    #4,D1
  4544.         andi.w    #$7F,D1
  4545. lbC000108
  4546.         move.w    D2,$74(A6,D1.W)
  4547.         or.b    $135(A6),D4
  4548.         move.w    D4,$76(A6,D1.W)
  4549.         move.w    D0,$72(A6)
  4550. ;        btst    #0,$3D(A6)
  4551. ;        bne.s    lbC00012C
  4552. ;        movea.l    $48(A6),A1
  4553. ;        tst.l    (A1)
  4554. ;        beq.s    lbC00012C
  4555. ;        bsr.l    *-$FFFF0278
  4556. ;lbC00012C
  4557.  
  4558.         MOVE.B    #$1,$BFEE01
  4559.  
  4560. ;        andi.b    #$BF,$BFEE01
  4561.  
  4562. lbC00012E
  4563.         MOVE.B    KBDQUALIFIER+1,d0
  4564.         TST.L    OSM_SLVTRAINER
  4565.         BEQ.S    .NOTRAINER
  4566.         MOVE.L    OSM_SLVTRAINER(PC),A0
  4567.         JSR    (A0)
  4568. .NOTRAINER
  4569.         MOVE.B    D0,KBDQUALIFIER+1
  4570.         BEQ.S    .1
  4571.         move.l    (_Slave),a0
  4572.         CMP.B    (ws_keyexit,a0),D0
  4573.         BEQ.S    .QUIT
  4574. .1
  4575.         movem.l    (SP)+,D2-D4/A6
  4576.         move.l    _inputhandler(PC),d0
  4577.         bne    _InputHandlerPI
  4578.         rts
  4579.  
  4580. .QUIT        PEA    TDREASON_OK
  4581.         MOVE.L    _RESLOAD(PC),-(A7)
  4582.         add.l    #resload_Abort,(a7)
  4583.         rts
  4584.  
  4585. ; < D1: keyboard raw code without press/release info
  4586. ; < D2: same thing with press/release info (bit #7)
  4587. ; > KBDQUALIFIER updated
  4588.  
  4589. _SetQualifierFlags:
  4590.     movem.l    D0/D1/A0,-(A7)
  4591.  
  4592.     sub.b    #$60,D1
  4593.     bcs.b    .exit
  4594.     cmp.b    #$8,D1
  4595.     bcc.b    .exit
  4596.     lea    .qualtable(pc),A0
  4597.     move.b    (A0,D1.W),D0
  4598.  
  4599.     btst    #7,D2
  4600.     bne.b    .release
  4601.     ; key pressed
  4602.  
  4603.     or.b    D0,KBDQUALIFIER
  4604.     bra.b    .exit
  4605. .release:
  4606.     not.b    D0
  4607.     and.b    D0,KBDQUALIFIER
  4608. .exit
  4609.     movem.l    (A7)+,A0/D1/D0
  4610.     rts
  4611.  
  4612. .qualtable:
  4613.     dc.b    IEQUALIFIER_LSHIFT,IEQUALIFIER_RSHIFT,IEQUALIFIER_CAPSLOCK
  4614.     dc.b    IEQUALIFIER_CONTROL,IEQUALIFIER_LALT,IEQUALIFIER_RALT
  4615.     dc.b    IEQUALIFIER_LCOMMAND,IEQUALIFIER_RCOMMAND
  4616.  
  4617.  
  4618.  
  4619. **************************************************************************
  4620.  
  4621.     include    "rawdofmt.s"
  4622.  
  4623. **************************************************************************
  4624.  
  4625. _GETCC:
  4626.     btst.b    #AFB_68010,(AttnFlags,A6)
  4627.     beq.b    .68000
  4628.     move    CCR,D0
  4629.     rts
  4630. .68000
  4631.     move    SR,D0
  4632.     rts
  4633.  
  4634. **************************************************************************
  4635. *   CIAB.RESOURCE                                                        *
  4636. **************************************************************************
  4637.  
  4638. CIABINIT    move.l    _ciabbase,d0
  4639.         beq    .init
  4640.         rts
  4641.  
  4642. .init        move.l    #-_LVOSetICR,d0
  4643.         move.l    #$34,d1
  4644.         lea    _ciabname(pc),a0
  4645.         bsr    _InitStruct
  4646.         move.l    d0,a0
  4647.         move.l    d0,_ciabbase
  4648.  
  4649.         patch    _LVOAddICRVector(a0),CIABADDICRV
  4650.         patch    _LVORemICRVector(a0),CIABREMICRV
  4651.         patch    _LVOAbleICR(a0),_ABLEICR
  4652.         patch    _LVOSetICR(a0),_SETICR
  4653.  
  4654.         move.l    #$BFD000,34(a0)    ; cia base v1.15
  4655.         clr.l    40(A0)        ; JOTD v1.15
  4656.         MOVE.B    #CIAB_INT_VAL,41(A0)
  4657.         MOVE.W    #INTF_EXTER,38(A0)
  4658.  
  4659.         clr.l    (a0)+
  4660.         clr.l    (a0)+
  4661.         clr.l    (a0)+
  4662.         clr.l    (a0)+
  4663.         clr.l    (a0)
  4664.  
  4665. ;;        MOVE.B    #CIA_COUNTER_INIT,$BFD500
  4666.         MOVE.B    #CIA_COUNTER_INIT,$BFD700
  4667.  
  4668.  
  4669.         rts
  4670.  
  4671.  
  4672. ; PutMsg, added by JOTD, implementation: AROS
  4673. ; < A0: MsgPort
  4674. ; < A1: Message
  4675.  
  4676. _PutMsg:
  4677.     DISABLE
  4678.  
  4679.     ; Set the node type to NT_MESSAGE == sent message
  4680.  
  4681.     move.b    #NT_MESSAGE,(LN_TYPE,A1)
  4682.     
  4683.     ; Add it to the message list.
  4684.  
  4685.     movem.l    A0/A1,-(A7)
  4686.     lea    (MP_MSGLIST,A0),A0
  4687.     bsr    _ADDTAIL
  4688.     movem.l    (A7)+,A0/A1
  4689.  
  4690.     move.l    (MP_SIGTASK,A0),D0
  4691.     beq.b    .notask
  4692.  
  4693.     ; And trigger the action
  4694. ;;    bsr    EnterDebugger
  4695. .notask
  4696.     ENABLE
  4697.     rts
  4698.  
  4699. ; GetMsg, added by JOTD, implementation: AROS
  4700. ; < A0: MsgPort
  4701. ; > D0: Message
  4702.  
  4703. _GetMsg:
  4704.     DISABLE
  4705.     lea    (MP_MSGLIST,A0),A0
  4706.     bsr    _REMHEAD    ; extracts and remove message from message port
  4707.     ENABLE
  4708.     rts
  4709.  
  4710. ; ReplyMsg, added by JOTD, implementation: AROS
  4711. ; 1> Places "NT_REPLYMSG" into LN_TYPE
  4712. ; 2> Puts the message to the port specified by MN_REPLYPORT
  4713. ;  If there is no replyport, sets LN_TYPE to "NT_FREEMSG"
  4714. ; < A1: Message
  4715.  
  4716. _ReplyMsg
  4717.     DISABLE
  4718.     move.l    (MN_REPLYPORT,A1),A0    ; check reply port
  4719.     cmp.l    #0,A0
  4720.     bne.b    .reply
  4721.     ; free message
  4722.  
  4723.     move.b    #NT_FREEMSG,(LN_TYPE,A1)
  4724.     bra    .exit
  4725.  
  4726. .reply
  4727.     ; Mark the message as replied
  4728.  
  4729.     move.b    #NT_REPLYMSG,(LN_TYPE,A1)
  4730.     ; Add it to the replyport's list
  4731.  
  4732.     movem.l    A0/A1,-(A7)
  4733.     lea    (MP_MSGLIST,A0),A0    ; reply port
  4734.     bsr    _ADDTAIL    
  4735.     movem.l    (A7)+,A0/A1
  4736.  
  4737.     move.l    (MP_SIGTASK,A0),D0
  4738.     beq.b    .notask
  4739.  
  4740.     ; And trigger the action
  4741. ;;    bsr    EnterDebugger
  4742. .notask
  4743. .exit    
  4744.     ENABLE
  4745.     rts
  4746.  
  4747. ; AddPort, AROS implementation + protection against NULL message list
  4748. ; < A1: port address to add
  4749.  
  4750. _AddPort:
  4751.     ; Yes, this is a messageport
  4752.  
  4753.     move.b    #NT_MSGPORT,(LN_TYPE,A1)
  4754.  
  4755.  
  4756.     IFEQ 1
  4757.     ; Clear the list of messages
  4758.  
  4759.     move.l    (MP_MSGLIST,A1),A0    ; A0 = message list
  4760.  
  4761.     cmp.l    #0,A0
  4762.     beq.b    .nomsglist
  4763.  
  4764.     movem.l    A0/A1,-(A7)
  4765.  
  4766.     lea    (LH_TAIL,A0),A1
  4767.     move.l    A1,(LH_HEAD,A0)
  4768.  
  4769.     clr.l    (LH_TAIL,A0)
  4770.  
  4771.     lea    (LH_HEAD,A0),A1
  4772.     move.l    A1,(LH_TAILPRED,A0)
  4773.  
  4774.     movem.l    (A7)+,A0/A1
  4775.     
  4776.  
  4777. .nomsglist
  4778.     ENDC
  4779.  
  4780.         ; Arbitrate for the list of messageports
  4781.     ; And add the actual port
  4782.  
  4783.  
  4784.     JSRLIB    Forbid
  4785.     lea    (PortList,A6),A0    ; system port list
  4786.     JSRLIB    Enqueue
  4787.  
  4788.     JSRLIB    Permit
  4789.     rts
  4790.  
  4791. ; WaitPort
  4792. ; < A0: port
  4793. ; > D0: 
  4794. _WaitPort:
  4795.     lea    (MP_MSGLIST,A0),A1    ; &port->mp_MsgList
  4796. .wait
  4797.     cmp.l    (LH_TAILPRED,A1),A1    ; while empty, loop
  4798.     bne.b    .wait            ; active loop ATM, no multitasking
  4799.  
  4800.     ; Return the first node in the list (may return NULL)
  4801.     
  4802.     move.l    (LH_HEAD,A1),D0
  4803.     rts
  4804.  
  4805. ; RemPort, AROS implementation
  4806. ; < A1: port address to remove
  4807.  
  4808. _RemPort:
  4809.     JSRLIB    Forbid
  4810.     JSRLIB    Remove
  4811.     JSRLIB    Permit
  4812.     rts
  4813.  
  4814.  
  4815. _Debug:
  4816.     bsr    EnterDebugger
  4817.     EMUFAIL    _LVODebug,_execname
  4818.  
  4819.  
  4820. EGSINIT:
  4821.     ; cannot open egs.library (Tower Assault)
  4822.     moveq.l    #0,D0
  4823.     RTS
  4824.  
  4825.  
  4826. ForeignAllocMem:
  4827.     move.l    A6,-(A7)
  4828.     move.l    $4.W,A6
  4829. ;;;    bset    #MEMB_CLEAR,D1    ; forces clear for internal OSEmu calls
  4830.     JSRLIB    AllocMem
  4831.     move.l    (A7)+,A6
  4832.     RTS
  4833.  
  4834. ForeignFreeMem:
  4835.     move.l    A6,-(A7)
  4836.     move.l    $4.W,A6
  4837.     JSRLIB    FreeMem
  4838.     move.l    (A7)+,A6
  4839.     RTS
  4840.  
  4841.  
  4842. AckKeyboard:
  4843.         movem.l    D0/D1,-(A7)
  4844.         BSET    #$06,$BFEE01
  4845.         MOVEQ.L    #2,D0
  4846. .4
  4847.         MOVE.B    $DFF006,D1
  4848. .3        CMP.B    $DFF006,D1
  4849.         BEQ.S    .3
  4850.         DBF    D0,.4
  4851.         BCLR    #$06,$BFEE01
  4852.  
  4853.     ;;    move.w    #0008,intreq+$DFF000    ; acknowledge interrupt
  4854.  
  4855.         movem.l    (A7)+,D0/D1
  4856.         rts
  4857.  
  4858. **************************************************************************
  4859. **************************************************************************
  4860.  
  4861.     INCLUDE    resources.s
  4862.     INCLUDE    dos.s
  4863.     INCLUDE    freeanim.s
  4864.     INCLUDE    nonvolatile.s
  4865.     INCLUDE    graphics.s
  4866.     INCLUDE    intuition.s
  4867.     INCLUDE    lowlevel.s
  4868.     INCLUDE    diskfont.s
  4869.  
  4870.     INCLUDE    utility.s
  4871.     INCLUDE    keymap.s
  4872.     INCLUDE    timer.s
  4873.     INCLUDE    console.s
  4874.     INCLUDE    locale.s
  4875.  
  4876.     INCLUDE    mathffp.s
  4877.     IFD    BIG_OSEMU    ; rather untested stuff there
  4878.     INCLUDE    mathtrans.s
  4879.     ENDC
  4880.  
  4881. **************************************************************************
  4882. **************************************************************************
  4883. *   CHIPMEMORYTABLE AND MEMORY MANAGEMENT DATA                           *
  4884. **************************************************************************
  4885.  
  4886. ;THIS HAS TO BE ABSOLUTELY THE LAST LABEL, THE TABLE HAS A LEN DEPENDING
  4887. ;  FROM THE SIZE OF THE MEMORY
  4888.     DC.B    'OSEMUEND'
  4889.     CNOP    0,8
  4890. _osemu_end
  4891. ALLOCMTAB
  4892.  
  4893.